带控件的WPF工具提示

Dav*_*lle 5 wpf tooltip

我有一个项目,我希望能够在某些控件上提供工具提示,这些控件将包含一些控件,如textbox和datepicker.我们的想法是拥有一些有限图形的弹出窗口,但有些控件可以互动.

我知道如何向控件添加"正常"工具提示,但是当您移动时,工具提示会消失,因此我无法与其进行交互.

这可能吗?如果是这样,如果没有,有什么替代方案吗?

谢谢

Fre*_*lad 10

你应该使用a Popup而不是aToolTip

例.Popup当鼠标移过时,A 会打开TextBox并且只要鼠标位于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>
Run Code Online (Sandbox Code Playgroud)

用的是 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

更新
要为单元格执行此操作,DataGrid您有几个选项.其中两个是Popup在DataTemplatesfor中添加一个DataGridTemplateColumn,或者你可以将它添加到DataGridCell Template.这是后来的一个例子.它需要你设置SelectionMode ="Single"和SelectionUnit ="Cell"DataGrid

<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>
Run Code Online (Sandbox Code Playgroud)