0

I have the following DataTemplate where inside is a Grid with a 'TextBlock' and TextBox that I want to align to the center:

<DataTemplate x:Key="OneSettingsEntryTemplate" DataType="{x:Type templateHelper:InputText}">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Column="0"
                   Style="{StaticResource StandardTextBlocksStyle}"
                   Text="{Binding Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
        <TextBox Grid.Column="1"
                 Style="{StaticResource DefaultTextBoxesStyle}"
                 Text="{Binding Content, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    </Grid>
</DataTemplate>

In one of my UserControls I have the following code where I use the Template:

<StackPanel DataContext="{Binding ElementName=ControlForProjectSettings, Path=ViewModel}"
            HorizontalAlignment="Center">
    <TextBlock Style="{StaticResource HeadingTextBlocksStyle}" 
               Text="Project settings"/>
    <ListView Background="#DAE7F5"
              ItemsSource="{Binding ProjectSettingEntries}"
              ItemTemplate="{StaticResource OneSettingsEntryTemplate}">
    </ListView>
</StackPanel>

When I run my application and display some items, the last item has a longer text and therefore the TextBox flips to the right. Application Image

I tried playing around with the HorizontalAlignment and VerticalAlignment properties inside the Grid or also using a DockPanel instead of a Grid but couldn't find a solution. Any ideas?

Stan1k
  • 338
  • 2
  • 17

1 Answers1

0

Thanks to this source: WPF share column width between separate grids I solved my problem.
I added the IsSharedSizeScope to my Grid and the SharedSizeGroup property to my columns.

    <Grid IsSharedSizeScope="True">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" SharedSizeGroup="A"/>
            <ColumnDefinition Width="Auto" SharedSizeGroup="A"/>
        </Grid.ColumnDefinitions>
Stan1k
  • 338
  • 2
  • 17