我的WPF应用程序中有一个对话框,其中包含一个ListBox。ListBox使用以下DataTemplate显示其内容:
<DataTemplate x:Key="AlarmClassTemplate">
<CheckBox Content="{Binding Path=Value}"
IsChecked="{Binding IsChecked}" />
</DataTemplate>我还配置了以下模板和样式,以便在ListBox's内容中出现错误时显示:
<ControlTemplate x:Key="InputErrorTemplateA">
<DockPanel LastChildFill="True">
<Image DockPanel.Dock="Right"
Height="30"
Margin="5"
Source="{StaticResource ErrorImage}"
ToolTip="Contains invalid data"
VerticalAlignment="Center"
Width="30" />
<Border BorderBrush="Red"
BorderThickness="5"
Margin="5">
<AdornedElementPlaceholder />
</Border>
</DockPanel>
</ControlTemplate>
<Style TargetType="{x:Type ListBox}">
<Setter Property="Validation.ErrorTemplate" Value="{StaticResource InputErrorTemplateA}" />
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="ToolTip">
<Setter.Value>
<Binding Path="(Validation.Errors).CurrentItem.ErrorContent" RelativeSource="{x:Static RelativeSource.Self}" />
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>下面是ListBox本身的XAML:
<ListBox FontSize="20"
FontWeight="Bold"
Grid.Column="1"
Grid.ColumnSpan="2"
Grid.Row="1"
Height="158"
ItemsSource="{Binding Path=IDs, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
ItemTemplate="{StaticResource AlarmClassTemplate}"
Margin="5,0,110,0"
Name="AlarmClassListBox"
ToolTip="{x:Static res:Car.EditDataRetention_AlarmClasses_ToolTip}"
Visibility="{Binding Converter={StaticResource BoolToVisibility}, Path=DataTypeIsAlarms}" />ListBox中数据的验证逻辑是,至少必须签出一个项。如果没有,ListBox应该显示一个错误,对话框中的OK按钮应该被禁用。
好消息是,当不检查ListBox中的任何内容时,对话框上的ListBox按钮确实被禁用。坏消息是,Style似乎不起作用,因为ListBox周围没有显示红色边框,错误图像(内有一个白色感叹号的红色圆圈)没有显示。
我在同一个对话框的其他控件上使用相同的ControlTempate和类似的Style,它们工作得很好。我做错了什么?是ListBox吗?ListBox验证的工作方式不同吗?
发布于 2014-03-14 17:09:25
我找到了问题的答案,在这个岗位上。结果是,当复选框更改时,我必须引发PropertyChanged事件,以便验证逻辑触发。由于ListBox中的项实现了INotifyPropertyChanged,因此很容易为每个项添加一个事件侦听器,因为它被添加到引发必要事件的ListBox中。
不管怎样,谢谢你。
发布于 2014-03-14 17:21:45
实际上,问题是您没有为验证被解雇而引发PropertyChanged事件。
但我可以在你的代码中看到另一个问题。您已经在这里为ListBox上的工具提示设置了本地值:
ToolTip="{x:Static res:Car.EditDataRetention_AlarmClasses_ToolTip}"但是您需要不同的工具提示,以防验证返回在样式触发器中定义的错误。
但是,本地值比style触发器具有更高的优先级。所以,你的工具提示永远不会被设置。因此,您应该将工具提示移动到样式设置器中:
<Setter Property="ToolTip"
Value="{x:Static res:Car.EditDataRetention_AlarmClasses_ToolTip}"/>MSDN链接- 依赖属性值优先.
https://stackoverflow.com/questions/22411327
复制相似问题