我需要有用户将定期附加/编辑的连接字符串列表。所以我定制了列表视图,它是数据绑定到一个可观察的集合。
<ListView x:Name="DGV_Env" Margin="5" FontSize="11" FontStyle="Italic" ItemsSource="{Binding Environments}" Background="White"
HorizontalContentAlignment="Center" VerticalContentAlignment="Center" SelectedIndex="0" SelectionMode="Single" >
<ListView.Resources>
<Style TargetType="ListViewItem">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="BorderThickness" Value="3" />
<Setter Property="BorderBrush" Value="Red" />
</Trigger>
<Trigger Property="Validation.HasError" Value="False">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="BorderBrush" Value="CadetBlue" />
</Trigger>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="TextBox">
<Setter Property="Validation.ValidationAdornerSite" Value="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem}}"/>
</Style>
<Style TargetType="PasswordBox">
<Setter Property="Validation.ValidationAdornerSite" Value="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem}}"/>
</Style>
</ListView.Resources>
<ListView.View>
<GridView x:Name="GV_Env_Column" AllowsColumnReorder="False">
<GridView.ColumnHeaderContextMenu>
<ContextMenu FontSize="12" Foreground="Black" />
</GridView.ColumnHeaderContextMenu>
<GridViewColumn Header="Slno" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Slno}" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="12" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Server">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Margin="-2,0,-2,0" BorderThickness="0" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Center" FontSize="12" Style="{StaticResource TextBoxStyle}" Tag="Server">
<TextBox.Text>
<Binding Mode="TwoWay" Path="Server" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True" >
<Binding.ValidationRules>
<local:NonEmptyStringValidator ValidatesOnTargetUpdated="True" ValidationStep="RawProposedValue" />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Database">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Margin="-2,0,-2,0" BorderThickness="0" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Center" FontSize="12" Style="{StaticResource TextBoxStyle}" Tag="Database">
<TextBox.Text>
<Binding Mode="TwoWay" Path="Database" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True" >
<Binding.ValidationRules>
<local:NonEmptyStringValidator ValidatesOnTargetUpdated="True" ValidationStep="RawProposedValue" />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="UserID">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Margin="-2,0,-2,0" BorderThickness="0" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Center" FontSize="12" Style="{StaticResource TextBoxStyle}" Tag="User ID">
<TextBox.Text>
<Binding Mode="TwoWay" Path="UserId" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True" >
<Binding.ValidationRules>
<local:NonEmptyStringValidator ValidatesOnTargetUpdated="True" ValidationStep="RawProposedValue" />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Password">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="White" Margin="-2,0,-2,0" FocusManager.FocusedElement="{Binding ElementName= ENV_Password}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<PasswordBox Tag="" Grid.Column="0" BorderThickness="0" Height="30" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" x:Name="ENV_Password">
<PasswordBox.Style>
</PasswordBox.Style>
</PasswordBox>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
我添加了 Binding.ValidationRules,它在字段为空时突出显示。现在我必须添加验证来验证连接字符串,如果任何失败都应该突出显示给用户以更改凭据或任何连接字符串参数。我尝试使用 INotifyDataErrorInfo 但无法成功或不知道如何正确使用它。它将整个列表视图突出显示为边框,并且对于有效的连接字符串也保持不变。
我需要向用户突出显示 UI 中有效或无效的连接字符串,而不是突出显示整个列表视图或抛出任何消息框。验证应该在后台发生,而不是通过任何其他事件(如按钮单击等)。这怎么可能正确实现。