[英]Creating a watermark templated texblock wpf
I have the following code for placing a "WaterMark"
TextBlock
on any wpf
control:我有以下代码用于在任何
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>
In order to prevent duplicating these lines for every control I want to bind
this TextBlock
to, I wish to create a UserControl
from it and use it like this:为了防止为我想将此
TextBlock
bind
到的每个控件复制这些行,我希望从中创建一个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"/>
(I have placed the TextBlock
definition in an included dll
file, and it looks like this: (我已经将
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>
The thing is that the DataTrigger
is binded to a control I currently have, in the application I'm developing: AvailableFrames , and if I wish to reuse this dll
in another application, I'll need to change the DataTrigger
binding.问题是
DataTrigger
绑定到我当前拥有的控件,在我正在开发的应用程序中: AvailableFrames ,如果我希望在另一个应用程序中重用这个dll
,我需要更改DataTrigger
绑定。
Can such a thing be done?这样的事情能做到吗?
In this concrete case NullToVisibilityConverter will be a simpler solution在这种具体情况下 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;
}
}
usage example:用法示例:
<ComboBox x:Name="FromFrameComboBox"/>
<TextBlock Visibility="{Binding SelectedItem, ElementName=FromFrameComboBox,
Converter={local:NullToVisibilityConverter NullValue=Visible, NotNullValue=Hidden}}"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.