0

Whenever a user enters an invalid value into a DataGridCell, I highlight the complete row and the specific cell especially. I found out that when I decrease the width of a column after validation, the red border of the invalid cell kepps its size/width. I.e. when I decrease the width of the cell/column the red border stays the old size and is now reaching into the subsequent column.

When I increase the width of the column, the border is adapted perfectly.
When I decrease the column width again it adapts the width of the border perfectly as well but only until the initial cell/column width. If I decrease the width further the border's minimum width is its initial width (which is the initial width of the cell/column).

How can I resize the border to the current column width?

<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>

Maybe setting MaxWidth=""? How can I do this?

Update 2019-01-30: I define this style in an external Style.xaml and its used in the same Style.xaml in another style:

<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>

which in turn is assigned to a DataGrid this way:

  <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>
du-it
  • 2,561
  • 8
  • 42
  • 80

0 Answers0