每当用户在DataGridCell中输入无效值时,我都会突出显示完整的行和特定的单元格。
我发现,在验证后我 减少 列的宽度时,无效单元格的红色边框会扭曲其大小/宽度。即当我减小单元格/列的宽度时,红色边框保持原来的大小,现在到达下一列。
当我增加列的宽度时,边框将完美地适应。
当我再次减小列的宽度时,它也可以完美地调整边框的宽度,但是直到初始单元格/列的宽度为止。如果我进一步减小宽度,则边框的最小宽度就是其初始宽度(这是单元格/列的初始宽度)。
如何将边框的大小调整为当前列的宽度?
<Setter Property="ValidationErrorTemplate">
<Setter.Value>
<ControlTemplate>
<Grid>
<Border BorderBrush="red" BorderThickness="1" Background="#11FF0000" Opacity="0.5" IsHitTestVisible="False" x:Name="errorBorder"/>
<Ellipse Width="12" Height="12" Fill="Red" Stroke="Black" StrokeThickness="0.5"/>
<TextBlock FontWeight="Bold" Padding="4,0,0,0" Margin="0" VerticalAlignment="Top" Foreground="White" Text="!" ToolTip="{Binding Notification.GetAllErrors}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
也许设置MaxWidth =“”?我该怎么办?
更新2019-01-30: 我在外部Style.xaml中定义此样式,并在另一样式中将其用于同一Style.xaml:
<Style x:Key="NotificationDataGridRow" TargetType="{x:Type DataGridRow}">
<Setter Property="FontSize" Value="16"/>
<Setter Property="Height" Value="Auto"/>
<Setter Property="FontFamily" Value="ArialMT"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=(Validation.HasError), RelativeSource={RelativeSource Self}}" Value="true" >
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="Red"/>
<Setter Property="IsEnabled" Value="True" />
<Setter Property="ValidationErrorTemplate">
<Setter.Value>
<ControlTemplate>
<Grid>
<Border BorderBrush="red" BorderThickness="1" Background="#11FF0000" Opacity="0.5" IsHitTestVisible="False" x:Name="errorBorder"/>
<Ellipse Width="12" Height="12" Fill="Red" Stroke="Black" StrokeThickness="0.5"/>
<TextBlock FontWeight="Bold" Padding="4,0,0,0" Margin="0" VerticalAlignment="Top" Foreground="White" Text="!" ToolTip="{Binding Notification.GetAllErrors}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Path=(Validation.HasError), RelativeSource={RelativeSource Self}}" Value="false" >
<Setter Property="ToolTip" Value="{x:Null}"/>
<Setter Property="IsEnabled" Value="True" />
</DataTrigger>
</Style.Triggers>
</Style>
依次将其分配给DataGrid:
<Grid KeyboardNavigation.TabNavigation="Local">
<ct_ctrls:CTDataGrid x:Name="tagsGrid" ItemsSource="{Binding}" GridLinesVisibility="Vertical" AlternatingRowBackground="#C3DDE5"
AutoGenerateColumns="False" CanUserAddRows="True" IsReadOnly="False"
SelectionUnit="Cell" SelectionMode="Extended" BorderThickness="3" RowStyle="{StaticResource RawTagDataGridRow}">
<DataGrid.Columns>
<DataGridTextColumn x:Name="TagName" Header="Tag" Width="*"
Binding="{Binding Mode=TwoWay, Path=RawTag.TagName, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}"
ElementStyle="{StaticResource ResourceKey=textBlockErrStyle}" />
<DataGridTextColumn x:Name="TagCycle" Header="Cycle"
Binding="{Binding Mode=TwoWay, Path=RawTag.Cycle, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}"
ElementStyle="{StaticResource ResourceKey=textBlockErrStyle}">
</DataGridTextColumn>
<DataGridTextColumn x:Name="TagSource" Header="Source" Width="*"
Binding="{Binding Mode=TwoWay, Path=RawTag.Source, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}"
ElementStyle="{StaticResource ResourceKey=textBlockErrStyle}"/>
<DataGridTextColumn x:Name="Unassigned" Header="unassigned" Width="*" KeyboardNavigation.TabIndex="4"
Binding="{Binding Mode=OneWay, Path=RawTag.Unassigned, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=False}"
ElementStyle="{StaticResource ResourceKey=textBlockUnassignedStyle}"/>
<DataGridTemplateColumn x:Name="editTagColumn" Header="" CanUserResize="True" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<WrapPanel>
<Button x:Name="btnTagDelete" Click="BtnTagDelete_Click" CommandParameter="{Binding}" Height="15" Width="15" Margin="2">
<Button.Content>
<Image Source="../Resources/delete.png"></Image>
</Button.Content>
</Button>
</WrapPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</ct_ctrls:CTDataGrid>
</Grid>