我有以下WPF用户界面:绑定到集合的列表视图。有一个datatemplate显示该集合中每个项目的特定数据。数据模板使用堆栈面板显示字段以及拥有绑定组。对于每个项目,用户将输入信息并进行验证。如果失败,则错误信息显示在堆栈面板中。除了在这里显示错误之外,我希望能够使用某种有问题的信息标记主机ListView。这样,我可以根据是否有错误影响用户界面的其余部分。如果我将绑定组移动到ListView,我将实现这一点。但是,我将丢失能够显示错误的stackpanel。
总之,我希望能够从数据模板到主机对象(在我的情况下是列表视图)中传播错误详细信息(或至少是指示错误的标志)。我只是尝试使用绑定来执行此操作,以便我不必编写任何代码。但我很难过如何做到这一点。
以下是代码的快速摘录:
<ListView Name="LiquidDetailsList" Grid.Row="2" DataContext="{Binding BatchToExecute}" ItemsSource="{Binding LiquidDefinitionList}" IsSynchronizedWithCurrentItem="True">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,5,0,0" Name="LiquidRecord">
<StackPanel.BindingGroup>
<BindingGroup Name="LiquidValidation" NotifyOnValidationError="True" />
</StackPanel.BindingGroup>
<StackPanel Orientation="Horizontal" >
<TextBox Name="BatchLiquidLocation" Width="65" Height="25" Margin="5,5,5,5" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Style="{StaticResource standardTextBoxErrorStyle}" Background="PowderBlue" SourceUpdated="LiquidComponent_SourceUpdated" TextChanged="LiquidComponent_TextChanged">
<Binding BindingGroupName="LiquidValidation" Path="Location" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" NotifyOnValidationError="True" />
</TextBox>
<TextBox Name="BatchLiquidVolume" Margin="10,5,0,5" Width="65" Height="25" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Style="{StaticResource standardTextBoxErrorStyle}" Background="PowderBlue" Tag="VOLUME" SourceUpdated="LiquidComponent_SourceUpdated" TextChanged="LiquidComponent_TextChanged">
<Binding BindingGroupName="LiquidValidation" Path="Volume" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" NotifyOnValidationError="True" />
</TextBox>
<CheckBox Name="BatchLiquidReuseTip" Margin="10,5,0,5" Width="25" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Background="PowderBlue" Tag="REUSETIP" VerticalAlignment="Center">
<CheckBox.IsChecked>
<Binding BindingGroupName="LiquidValidation" Path="ReuseTip" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" NotifyOnValidationError="True" />
</CheckBox.IsChecked>
</CheckBox>
<Button Name="DeleteLiquidButton" Margin="10,5,5,5" Width="25" Height="25" Click="DeleteLiquidButton_Click" HorizontalAlignment="Center">X</Button>
</StackPanel>
<ItemsControl Margin="10,0,0,0" ItemsSource="{Binding Path=(Validation.Errors), ElementName=LiquidRecord}" IsTabStop="False">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Label Foreground="Red" Content="{Binding Path=ErrorContent}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>