Ala*_*yne 3 wpf xaml datatrigger
我有多个以下形式的数据触发器:
<Style x:Key="VerifyCheckBox" TargetType="{x:Type CheckBox}">
<Setter Property="Height" Value="14" />
<Setter Property="FontSize" Value="12" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=PrimaryInsuranceCompany, Path=(Validation.HasError)}" Value="True">
<Setter Property="IsEnabled" Value="False" />
<Setter Property="IsChecked" Value="False" />
</DataTrigger>
Run Code Online (Sandbox Code Playgroud)
CheckBoxIsEnabled上的属性设置 正确。但是,如果一开始就手动检查过该属性,则该属性不会被取消检查。IsChecked
可以IsChecked从 Setter 中取消选中吗?
编辑#1
To complicate matters a bit more, the checkbox is bound to a property in my viewmodel as:
<CheckBox
Style="{StaticResource VerifyCheckBox}"
IsChecked="{Binding PrimaryInsurance.SelectedInsurance.Verify}"
Content="Verify" HorizontalAlignment="Left" Margin="789,92,0,676" Width="46" />
Run Code Online (Sandbox Code Playgroud)
Style.Triggers 中有多个 DataTrigger,每个 DataTrigger 检查 UI 上的不同元素。简而言之,UI 具有所有数据验证功能。例如,上面命名的元素 PrimaryInsuranceCompany 是:
<wc:AutoFilteredComboBox
Name="PrimaryInsuranceCompany"
IsEnabled="{Binding PrimaryInsurance.Enabled}"
ItemsSource="{Binding PrimaryInsurance.AllCompanies}"
ItemTemplate="{StaticResource CompanyTemplate}"
IsEditable="True"
IsCaseSensitive="False"
IsTextSearchEnabled="True"
TextSearch.TextPath="Companyname"
Text="{Binding PrimaryInsurance.NewCompanyName, UpdateSourceTrigger=PropertyChanged}"
theFilter="{Binding PrimaryInsurance.TheFilter}"
SelectedItem="{Binding PrimaryInsurance.SelectedInsurance.Company}"
h:ComboBoxRegexValidator.RegexText="{x:Static h:RegexLibrary.NonEmptyRegex}"
HorizontalAlignment="Left" Margin="590,138,0,0" VerticalAlignment="Top" Width="363" />
Run Code Online (Sandbox Code Playgroud)
因此,特别是对于组合框元素,我试图避免在视图模型中重复验证过程,因为它已经直接在视图中完成。这可以做到吗?
这常常会引起混乱。您可能想阅读以下内容:https://msdn.microsoft.com/en-us/library/vstudio/ms743230%28v=vs.100%29.aspx。基本上,wpf 中的依赖属性可能在不同的位置设置:本地 (IsChecked=True)、样式、触发器等。当在多个位置设置属性时,将使用特定的解决顺序来解决冲突。这是一个例子:
<Window.Resources>
<Style x:Key="VerifyCheckBox" TargetType="{x:Type CheckBox}">
<Setter Property="Height" Value="14" />
<Setter Property="FontSize" Value="12" />
<Style.Triggers>
<DataTrigger Binding="{Binding TestValue}" Value="True">
<Setter Property="IsChecked" Value="False" />
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<CheckBox Content="Hey there" IsChecked="True" Style="{StaticResource VerifyCheckBox}" />
Run Code Online (Sandbox Code Playgroud)
这里我们在两个地方设置 IsChecked :本地(IsChecked=True)和触发器中。本地值具有更高的优先级,因此,当 TestValue 变为 true 时,复选框将不会被取消选中。但是我们没有在本地设置 IsEnabled,因此它将具有来自触发器的值。请注意,如果我们确实在本地设置了IsEnabled=True,则触发器根本不会产生任何效果。
现在让我们尝试这样:
<Window.Resources>
<Style x:Key="VerifyCheckBox" TargetType="{x:Type CheckBox}">
<Setter Property="Height" Value="14" />
<Setter Property="FontSize" Value="12" />
<Setter Property="IsChecked" Value="True"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding TestValue}" Value="True">
<Setter Property="IsChecked" Value="False" />
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<CheckBox Content="Hey there" Style="{StaticResource VerifyCheckBox}" />
Run Code Online (Sandbox Code Playgroud)
我们在样式中移动了 IsChecked 初始设置器,现在它按预期工作,因为触发器优先于样式设置器中的值。
更新:用户点击不会改变上述行为。因此,如果您本地 IsChecked=True,则用户单击两次,则 TestValue 变为 true - 它不会被取消选中。如果您执行相同的操作,但在样式设置器中初始化 IsChecked=True - 它将被取消选中。
| 归档时间: |
|
| 查看次数: |
1678 次 |
| 最近记录: |