使用绑定 WPF 在控件触发器中设置 属性
Set a property in Control Triggers using Binding WPF
我在WPF中设计了一个TextBox,它可以自动验证并在验证失败时显示错误消息。
这是我的控件模板:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Controls:ShamsText">
<Grid>
<Border Name="container" HorizontalAlignment="Left" Padding="{TemplateBinding Padding}" Height="{TemplateBinding Height}" BorderThickness="1" BorderBrush="#ccc" Background="{TemplateBinding Background}">
<ScrollViewer Padding="0" x:Name="PART_ContentHost" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" TextBlock.TextAlignment="Center" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
<Popup Name="Popup"
Placement="Top"
Focusable="False"
AllowsTransparency="True"
IsOpen="{TemplateBinding ComboBox.IsDropDownOpen}"
PopupAnimation="Slide">
<Grid>
<Border Padding="0 0 0 10">
<Border Name="DropDownBorder"
Background="#35403F"
Padding="12 10 12 10"
Margin="0, 1, 0, 0"
SnapsToDevicePixels="True"
CornerRadius="4"
BorderThickness="1,1,1,1"
BorderBrush="#35403F">
<ScrollViewer SnapsToDevicePixels="True">
<TextBlock Name="PopupText" FontWeight="Thin" TextWrapping="Wrap" MaxWidth="{TemplateBinding Width}" Text=" " Foreground="#E8E8E8"/>
</ScrollViewer>
</Border>
</Border>
<TextBlock Foreground="#35403F" VerticalAlignment="Bottom" Margin="15 0 0 0" Text="▼"></TextBlock>
</Grid>
</Popup>
</Grid>
<ControlTemplate.Triggers>
这是我在它的 ControlTemplate 上的触发器:
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsShowRequired" Value="True"></Condition>
<Condition Property="IsFocused" Value="True"></Condition>
<Condition Property="IsReadOnly" Value="False"></Condition>
<Condition Property="Text" Value=""></Condition>
</MultiTrigger.Conditions>
<Setter TargetName="container" Property="BorderBrush" Value="#66AFE9"/>
<Setter TargetName="PopupText" Property="Text" Value="Required Error Messagge"/>
<Setter TargetName="Popup" Property="IsOpen" Value="True"></Setter>
</MultiTrigger>
我想使“Required Error Message
”可自定义,所以我在我的文本框 Class 中添加了一个 属性:
public static readonly DependencyProperty RequiredMessageProperty = DependencyProperty.Register("RequiredMessage", typeof(string), typeof(ShamsText), new PropertyMetadata("این مقدار اجباری است."));
public string RequiredMessage
{
get { return (string)GetValue(RequiredMessageProperty); }
set { SetValue(RequiredMessageProperty, value); }
}
所以我将我的多触发器更改为:
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsShowRequired" Value="True"></Condition>
<Condition Property="IsFocused" Value="True"></Condition>
<Condition Property="IsReadOnly" Value="False"></Condition>
<Condition Property="Text" Value=""></Condition>
</MultiTrigger.Conditions>
<Setter TargetName="container" Property="BorderBrush" Value="#66AFE9"/>
<Setter TargetName="PopupText" Property="Text" Value="{Binding RequiredMessage}"/>
<Setter TargetName="Popup" Property="IsOpen" Value="True"></Setter>
</MultiTrigger>
但只要触发此触发器,它就会将 PopupText 的文本设置为 null。
如何动态设置这个值?
问题出在您的 Binding
,使用您的代码时出现绑定错误。将您的绑定更改为:
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsShowRequired" Value="True"></Condition>
<Condition Property="IsFocused" Value="True"></Condition>
<Condition Property="IsReadOnly" Value="False"></Condition>
<Condition Property="Text" Value=""></Condition>
</MultiTrigger.Conditions>
<Setter TargetName="container" Property="BorderBrush" Value="#66AFE9"/>
<Setter TargetName="PopupText" Property="Text" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=RequiredMessage}"/>
<Setter TargetName="Popup" Property="IsOpen" Value="True"></Setter>
</MultiTrigger>
我在WPF中设计了一个TextBox,它可以自动验证并在验证失败时显示错误消息。
这是我的控件模板:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Controls:ShamsText">
<Grid>
<Border Name="container" HorizontalAlignment="Left" Padding="{TemplateBinding Padding}" Height="{TemplateBinding Height}" BorderThickness="1" BorderBrush="#ccc" Background="{TemplateBinding Background}">
<ScrollViewer Padding="0" x:Name="PART_ContentHost" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" TextBlock.TextAlignment="Center" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
<Popup Name="Popup"
Placement="Top"
Focusable="False"
AllowsTransparency="True"
IsOpen="{TemplateBinding ComboBox.IsDropDownOpen}"
PopupAnimation="Slide">
<Grid>
<Border Padding="0 0 0 10">
<Border Name="DropDownBorder"
Background="#35403F"
Padding="12 10 12 10"
Margin="0, 1, 0, 0"
SnapsToDevicePixels="True"
CornerRadius="4"
BorderThickness="1,1,1,1"
BorderBrush="#35403F">
<ScrollViewer SnapsToDevicePixels="True">
<TextBlock Name="PopupText" FontWeight="Thin" TextWrapping="Wrap" MaxWidth="{TemplateBinding Width}" Text=" " Foreground="#E8E8E8"/>
</ScrollViewer>
</Border>
</Border>
<TextBlock Foreground="#35403F" VerticalAlignment="Bottom" Margin="15 0 0 0" Text="▼"></TextBlock>
</Grid>
</Popup>
</Grid>
<ControlTemplate.Triggers>
这是我在它的 ControlTemplate 上的触发器:
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsShowRequired" Value="True"></Condition>
<Condition Property="IsFocused" Value="True"></Condition>
<Condition Property="IsReadOnly" Value="False"></Condition>
<Condition Property="Text" Value=""></Condition>
</MultiTrigger.Conditions>
<Setter TargetName="container" Property="BorderBrush" Value="#66AFE9"/>
<Setter TargetName="PopupText" Property="Text" Value="Required Error Messagge"/>
<Setter TargetName="Popup" Property="IsOpen" Value="True"></Setter>
</MultiTrigger>
我想使“Required Error Message
”可自定义,所以我在我的文本框 Class 中添加了一个 属性:
public static readonly DependencyProperty RequiredMessageProperty = DependencyProperty.Register("RequiredMessage", typeof(string), typeof(ShamsText), new PropertyMetadata("این مقدار اجباری است."));
public string RequiredMessage
{
get { return (string)GetValue(RequiredMessageProperty); }
set { SetValue(RequiredMessageProperty, value); }
}
所以我将我的多触发器更改为:
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsShowRequired" Value="True"></Condition>
<Condition Property="IsFocused" Value="True"></Condition>
<Condition Property="IsReadOnly" Value="False"></Condition>
<Condition Property="Text" Value=""></Condition>
</MultiTrigger.Conditions>
<Setter TargetName="container" Property="BorderBrush" Value="#66AFE9"/>
<Setter TargetName="PopupText" Property="Text" Value="{Binding RequiredMessage}"/>
<Setter TargetName="Popup" Property="IsOpen" Value="True"></Setter>
</MultiTrigger>
但只要触发此触发器,它就会将 PopupText 的文本设置为 null。
如何动态设置这个值?
问题出在您的 Binding
,使用您的代码时出现绑定错误。将您的绑定更改为:
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsShowRequired" Value="True"></Condition>
<Condition Property="IsFocused" Value="True"></Condition>
<Condition Property="IsReadOnly" Value="False"></Condition>
<Condition Property="Text" Value=""></Condition>
</MultiTrigger.Conditions>
<Setter TargetName="container" Property="BorderBrush" Value="#66AFE9"/>
<Setter TargetName="PopupText" Property="Text" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=RequiredMessage}"/>
<Setter TargetName="Popup" Property="IsOpen" Value="True"></Setter>
</MultiTrigger>