WPF带控件的工具提示

5
我有一个项目,希望在一些控件上添加提示框,包括文本框和日期选择器等控件。想法是创建一个弹出窗口,其中有一些控件供交互,但图形方面很简单。
我知道如何为控件添加“普通”提示框,但当移动时,提示框会消失,无法进行交互。
这是否可能?如果可以,如何实现?如果不行,有没有其他的选择?
谢谢。
1个回答

11
你应该使用 Popup 而不是 ToolTip
例如,当鼠标移动到 TextBox 上时,会打开一个 Popup,并在鼠标停留在 TextBoxPopup 上时保持打开状态。
<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 中为单元格执行此操作,您有几个选项。其中两个选项是在 DataGridTemplateColumnDataTemplates 中添加一个 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>

这个可以在DatagridRow上完成吗,还是应该使用一个带有textblock的TemplateColumn? - David Brunelle
当然,您希望它如何工作? Popup 应该在什么时候可见等等? - Fredrik Hedblad
当选中单元格时,我想要打开弹出窗口。这样做后,将会有一个带有选项的小弹出窗口可用。 - David Brunelle
DataGridCell 模板中添加了一个带有 Popup 的示例,该示例将在所选单元格的右侧打开。 - Fredrik Hedblad
BooleanOrConverter 应该放在哪里?尽管我已经将它放在显示工具提示的窗口类中,但仍然出现找不到的异常。 - Arsen Zahray
不错的帖子。帮助我创建了一个带有其他子控件的自定义弹出框。非常感谢。 - Vikram

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接