我有一个项目,希望在一些控件上添加提示框,包括文本框和日期选择器等控件。想法是创建一个弹出窗口,其中有一些控件供交互,但图形方面很简单。
我知道如何为控件添加“普通”提示框,但当移动时,提示框会消失,无法进行交互。
这是否可能?如果可以,如何实现?如果不行,有没有其他的选择?
谢谢。
我知道如何为控件添加“普通”提示框,但当移动时,提示框会消失,无法进行交互。
这是否可能?如果可以,如何实现?如果不行,有没有其他的选择?
谢谢。
Popup
而不是 ToolTip
。TextBox
上时,会打开一个 Popup
,并在鼠标停留在 TextBox
或 Popup
上时保持打开状态。<TextBox Name="textBox"
Text="Popup On Mouse Over"
HorizontalAlignment="Left"/>
<Popup PlacementTarget="{Binding ElementName=textBox}"
Placement="Bottom">
<Popup.IsOpen>
<MultiBinding Mode="OneWay" Converter="{StaticResource BooleanOrConverter}">
<Binding Mode="OneWay" ElementName="textBox" Path="IsMouseOver"/>
<Binding RelativeSource="{RelativeSource Self}" Path="IsMouseOver" />
</MultiBinding>
</Popup.IsOpen>
<StackPanel>
<TextBox Text="Some Text.."/>
<DatePicker/>
</StackPanel>
</Popup>
它使用了一个BooleanOrConverter
public class BooleanOrConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
foreach (object booleanValue in values)
{
if (booleanValue is bool == false)
{
throw new ApplicationException("BooleanOrConverter only accepts boolean as datatype");
}
if ((bool)booleanValue == true)
{
return true;
}
}
return false;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
更新
要在 DataGrid
中为单元格执行此操作,您有几个选项。其中两个选项是在 DataGridTemplateColumn
的 DataTemplates
中添加一个 Popup
,或者将其添加到 DataGridCell Template
中。这是后一种选项的示例。它需要您在 DataGrid
上设置 SelectionMode="Single" 和 SelectionUnit="Cell"。
<DataGrid SelectionMode="Single"
SelectionUnit="Cell"
...>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid>
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
<Popup PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
Placement="Right"
IsOpen="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected}">
<StackPanel>
<TextBox Text="Some Text.."/>
<DatePicker/>
</StackPanel>
</Popup>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.CellStyle>
<!--...-->
</DataGrid>
Popup
应该在什么时候可见等等? - Fredrik HedbladDataGridCell
模板中添加了一个带有Popup
的示例,该示例将在所选单元格的右侧打开。 - Fredrik Hedblad