创建水印模板文本块 wpf

Creating a watermark templated texblock wpf

我有以下代码用于在任何 wpf 控件上放置 "WaterMark" TextBlock

<TextBlock Text="Some Text" IsHitTestVisible="False" Foreground="Gray" HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="Normal">
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Setter Property="Visibility" Value="Hidden"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=FromFrameComboBox, Path=SelectedItem}" Value="{x:Null}">
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

为了防止我想要 bind 这个 TextBlock 到的每个控件重复这些行,我希望从它创建一个 UserControl 并像这样使用它:

<ComboBox x:Name="FromFrameComboBox" Grid.Row="0" Grid.Column="1" ItemsSource="{Binding OrDataContainers.ObjectsCollection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalContentAlignment="Center" 
                              VerticalContentAlignment="Center" VerticalAlignment="Center" SelectedItem="{Binding OrDataContainers.SelectedObject, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="0,5,0,0"/>
<Wpf:WatermarkTextBlock x:Name="FromFrameComboBoxText" Text="Select Frame Number" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center"/>

(我已将 TextBlock 定义放在包含的 dll 文件中,它看起来像这样:

<TextBlock x:Class="Wpf.WatermarkTextBlock" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       IsHitTestVisible="False" Foreground="Gray" FontWeight="Normal">
<TextBlock.Style>
    <Style TargetType="TextBlock">
        <Setter Property="Visibility" Value="Hidden"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=AvailableFrames, Path=SelectedItem}" Value="{x:Null}">
                <Setter Property="Visibility" Value="Visible"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</TextBlock.Style>

问题是 DataTrigger 绑定到我目前拥有的控件,在我正在开发的应用程序中:AvailableFrames,如果我想重用这个 dll 在另一个应用程序中,我需要更改 DataTrigger 绑定。

这样的事情能做吗?

在这个具体案例中,NullToVisibilityConverter 将是一个更简单的解决方案

public class NullToVisibilityConverter : MarkupExtension, IValueConverter
{
    public Visibility NullValue { get; set; }
    public Visibility NotNullValue { get; set; }
    public NullToVisibilityConverter()
    {
        NullValue = Visibility.Collapsed;
        NotNullValue = Visibility.Visible;
    }
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
            return NullValue;
        return NotNullValue;
    }


    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}

用法示例:

<ComboBox x:Name="FromFrameComboBox"/>
<TextBlock Visibility="{Binding SelectedItem, ElementName=FromFrameComboBox, 
                                Converter={local:NullToVisibilityConverter NullValue=Visible, NotNullValue=Hidden}}"/>