无法在网格内正确对齐文本框
Cannot align textboxes inside grid properly among themselves
我有以下 DataTemplate
,其中有一个 Grid
和一个 'TextBlock' 和 TextBox
我想对齐到中心:
<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>
在我的 UserControls
之一中,我有以下使用模板的代码:
<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>
当我 运行 我的应用程序显示一些项目时,最后一个项目的文本较长,因此文本框向右翻转。 Application Image
我尝试在 Grid 中使用 HorizontalAlignment
和 VerticalAlignment
属性,或者也尝试使用 DockPanel
而不是 Grid 但找不到解决方案。有什么想法吗?
感谢这个来源:WPF share column width between separate grids我解决了我的问题。
我将 IsSharedSizeScope
添加到我的 Grid
并将 SharedSizeGroup
属性 添加到我的列。
<Grid IsSharedSizeScope="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="A"/>
<ColumnDefinition Width="Auto" SharedSizeGroup="A"/>
</Grid.ColumnDefinitions>
我有以下 DataTemplate
,其中有一个 Grid
和一个 'TextBlock' 和 TextBox
我想对齐到中心:
<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>
在我的 UserControls
之一中,我有以下使用模板的代码:
<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>
当我 运行 我的应用程序显示一些项目时,最后一个项目的文本较长,因此文本框向右翻转。 Application Image
我尝试在 Grid 中使用 HorizontalAlignment
和 VerticalAlignment
属性,或者也尝试使用 DockPanel
而不是 Grid 但找不到解决方案。有什么想法吗?
感谢这个来源:WPF share column width between separate grids我解决了我的问题。
我将 IsSharedSizeScope
添加到我的 Grid
并将 SharedSizeGroup
属性 添加到我的列。
<Grid IsSharedSizeScope="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="A"/>
<ColumnDefinition Width="Auto" SharedSizeGroup="A"/>
</Grid.ColumnDefinitions>