我有一个问题。我希望将ListBox
与DataTemplate
作为Grid
。该网格有2列。我想将第一列宽度设置为3*
,将另一列宽度设置为*
。这该怎么做?我会复制我的代码。
<ListBox x:Name="commandListbox" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding}"/>
<TextBlock Grid.Column="1" Text="icon" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
答案 0 :(得分:6)
最好将模板存储在资源中:
<Window.Resources>
<DataTemplate x:Key="DefaultTemplate">
<Grid x:Name="GridItem" Width="200">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock x:Name="Parameter" Grid.Column="1" Text="{Binding Path=Name}" Margin="5,1,0,0" />
<TextBlock x:Name="Value" Grid.Column="2" Text="{Binding Path=Age}" Margin="85,1,0,0" />
<Line x:Name="Separator" X1="0" X2="0" Y1="0" Y2="20" SnapsToDevicePixels="True" Grid.Column="1" Stroke="Black" StrokeThickness="2" Margin="50,0,0,0" HorizontalAlignment="Left" />
</Grid>
</DataTemplate>
</Window.Resources>
ListBox define:
<ListBox Name="MyListBox" ItemTemplate="{StaticResource DefaultTemplate}" />
在代码C#中:
public class Person
{
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
}
定义ObservableCollection:
private ObservableCollection<Person> MyListBoxData = new ObservableCollection<Person>();
在集合上添加项目:
MyListBoxData.Add(new Person()
{
Name = "Nick",
Age = 21,
});
MyListBoxData.Add(new Person()
{
Name = "Adam",
Age = 11,
});
MyListBox.ItemsSource = MyListBoxData;
<强>编辑:强>
然后设置第一个TextBlock的Width =“3 *”,Width =“*”和Margin =“ - WidthGrid”:
<Grid x:Name="GridItem" Width="300">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock x:Name="Parameter" Grid.Column="1" Text="{Binding Path=Name}" Margin="-220,0,0,0" />
<TextBlock x:Name="Value" Grid.Column="2" Text="{Binding Path=Age}" Margin="0,0,0,0" />
</Grid>
答案 1 :(得分:2)
使用IsSharedSizeScope在生成的网格上共享列大小调整,用于自动宽度(不能为*大小调整):
通过http://wpftutorial.net/DataTemplates.html
<ListBox ItemsSource="{Binding}" BorderBrush="Transparent"
Grid.IsSharedSizeScope="True"
HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Key" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}" FontWeight="Bold" />
<TextBox Grid.Column="1" Text="{Binding Value }" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
参与大小分享的列和行不尊重星号大小。在此方案中,Star大小调整被视为自动。 如果在资源模板中将IsSharedSizeScope设置为true,则网格大小共享不起作用,并且在该模板之外定义SharedSizeGroup。
答案 2 :(得分:1)
<ListBox x:Name="commandListbox" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.75*" />
<ColumnDefinition Width="0.25*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding}"/>
<TextBlock Grid.Column="1" Text="icon" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
如果我理解正确的话,你正在寻找第一列的3/4空间和第二列的1/4空间。以上述格式设置宽度必须解决问题。
希望它能解决。