[WPF]如何设置数据模板中网格的宽度和高度
有以下wpf代码:
<Window x:Class="WpfApplication5.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:WpfApplication5"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<c:Places x:Key="PlacesData"/>
<DataTemplate x:Key="DataTemplate" DataType="{x:Type c:Place}">
<Grid HorizontalAlignment="Left"
>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="40"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Name}"/>
<TextBlock Grid.Column="1" Text="{Binding State}" TextAlignment="Right"/>
</Grid>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox ItemsSource="{Binding Source={StaticResource PlacesData}}"
ItemTemplate="{StaticResource DataTemplate}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.CanContentScroll="False"
HorizontalContentAlignment="Stretch"/>
</Grid>
我希望状态代码始终显示在列表框的右侧,如果我调整窗口大小,也必须发生这种情况。
有什么想法吗?
Having the following wpf code:
<Window x:Class="WpfApplication5.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:WpfApplication5"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<c:Places x:Key="PlacesData"/>
<DataTemplate x:Key="DataTemplate" DataType="{x:Type c:Place}">
<Grid HorizontalAlignment="Left"
>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="40"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Name}"/>
<TextBlock Grid.Column="1" Text="{Binding State}" TextAlignment="Right"/>
</Grid>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox ItemsSource="{Binding Source={StaticResource PlacesData}}"
ItemTemplate="{StaticResource DataTemplate}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.CanContentScroll="False"
HorizontalContentAlignment="Stretch"/>
</Grid>
I want that the state code to be displayed always in the right side of the listbox and this must happen also if I resize the window.
Any ideas ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保每个
ListBoxItem
的HorizontalContentAlignment
设置为Stretch
:Ensure the
HorizontalContentAlignment
of eachListBoxItem
is set toStretch
:使网格的
HorizontalAlignment
Stretch
,而不是Left
。Make your Grid's
HorizontalAlignment
Stretch
, notLeft
.