Border in error template not showing for entire control

192 Views Asked by At

I have given an error template for a textbox. But the template is not rendering correctly. The width of the border is that of the textblock in the previous row. It renders as shown below

enter image description here

But when I scroll the mouse or maximize the window, the border shows up around the entire textbox

enter image description here

My Error Template:

<ControlTemplate x:Key="TextBoxErrorStyle">
    <AdornedElementPlaceholder>
        <Border BorderBrush="Red" BorderThickness="1"/>
    </AdornedElementPlaceholder>
</ControlTemplate>

This textbox is part of a grid in a list box item template. The list box is part of another grid on which has Grid.IsSharedSizeScope set to true.

<ListBox ItemsSource="{Binding MyCollection}" 
             VerticalAlignment="Top"
             HorizontalContentAlignment="Stretch"
             Margin="10,100,22,100"
             ScrollViewer.CanContentScroll="False">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border BorderThickness="1" CornerRadius="2" BorderBrush="LightGray">
                    <DockPanel LastChildFill="True">
                        <Grid DockPanel.Dock="Left">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="Auto" SharedSizeGroup="sharedColumn"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <TextBlock Grid.Column="0" Grid.Row="0" FontStyle="Italic" Text="Class" Margin="4 7 4 4"/>
                            <TextBlock Grid.Column="1" Grid.Row="0" FontStyle="Italic" Text="{Binding Row1 }" Margin="4 7 4 4" />

                            <TextBlock Grid.Column="0" Grid.Row="1" Text="Row 2"   Margin="4 11 4 4"/>
                            <TextBox Grid.Column="1" 
                                     Grid.Row="1" 
                                     Text="{Binding Quantity , UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True, FallbackValue='0'}" 
                                     Validation.ErrorTemplate="{StaticResource TextBoxErrorStyle}"
                                     Margin="4 11 4 4" />
                        </Grid>
                    </DockPanel>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

Update:

If I remove the SharedSizeGroup and fix the width of the column, it works correctly. But I cant fix the column width like that

0

There are 0 best solutions below