![](/img/trans.png)
[英]How can I render a WPF Grid that has 14 equal sized columns and 1 column 30% the size of the previous equal columns
[英]How Do I Make a WPF Grid Containing an ItemsControl Have Columns of Equal Size
這里和這里有類似的問題,但我已經嘗試了兩者中的所有內容,但它們並沒有解決我的問題。
我想在WPF(布局)網格中的屏幕上顯示一個復選框的網格。 這些復選框具有可變長度文本。 我希望所有列都具有相同的寬度,並且它們都顯示文本而不截斷。 也就是說,我需要它們都具有最長文本的寬度。 我希望屏幕本身的大小適合內容。 也就是說,要足夠寬以顯示所有復選框。
復選框列表根據其他因素而變化,因此我使用ItemsControl來顯示它們。 下面是代碼的簡化版本。
如果運行此列,帶有“長文本”(第二列)的列比具有較短文本的列寬得多。 為了使它們具有相同的寬度,我嘗試使用SharedSizeGroup,並嘗試在列加載事件中設置MaxWidth = ActualWidth,這是其他地方建議的解決方案,但這些都不起作用。
我肯定必須有一個簡單的答案,這似乎是一個相當基本的事情想要做一個布局控制。 我做錯了什么?
XAML:
<Window x:Class="GridColsEqualSize.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" SizeToContent="WidthAndHeight" FontSize="25">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" /> <!-- In the real project second row is for OK/Cancel buttons -->
</Grid.RowDefinitions>
<ItemsControl ItemsSource="{Binding CheckBoxItems}" Grid.Row="0" Grid.Column="0">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid>
<Grid.ColumnDefinitions>
<!--Whatever I do I can't get the screen to resize and the cols to have the same width-->
<ColumnDefinition Width="Auto" SharedSizeGroup="A" />
<ColumnDefinition Width="Auto" SharedSizeGroup="A" />
<ColumnDefinition Width="Auto" SharedSizeGroup="A" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Text}" IsChecked="{Binding Enabled}"
Margin="0,0,10,0"></CheckBox>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemContainerStyle>
<Style>
<Style.Setters>
<Setter Property="Grid.Row" Value="{Binding GridRow}" />
<Setter Property="Grid.Column" Value="{Binding GridColumn}" />
</Style.Setters>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
</Grid>
</Window>
C#:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
CheckBoxItems = new List<CheckBoxItem>
{
new CheckBoxItem("A", true),
new CheckBoxItem("B"),
new CheckBoxItem("C", true),
new CheckBoxItem("D"),
new CheckBoxItem("E", true),
new CheckBoxItem("F"),
new CheckBoxItem("G"),
new CheckBoxItem("Long text", true),
new CheckBoxItem("")
};
SetCheckBoxItemRowsAndColumns();
}
public List<CheckBoxItem> CheckBoxItems { get; set; }
private void SetCheckBoxItemRowsAndColumns()
{
int currentColumn = 0;
int currentRow = 0;
foreach (CheckBoxItem checkBoxItem in CheckBoxItems)
{
checkBoxItem.GridColumn = currentColumn;
checkBoxItem.GridRow = currentRow;
if (currentColumn != 2)
{
currentColumn++;
}
else
{
currentRow++;
currentColumn = 0;
}
}
}
public class CheckBoxItem
{
public CheckBoxItem(string text, bool enabled = false)
{
Text = text;
Enabled = enabled;
}
public string Text { get; set; }
public bool Enabled { get; set; }
public int GridRow { get; set; }
public int GridColumn { get; set; }
}
private void FrameworkContentElement_OnLoaded(object sender, RoutedEventArgs e)
{
((ColumnDefinition)sender).MaxWidth = ((ColumnDefinition)sender).ActualWidth;
}
}
一切都很好:)你選擇了正確的解決方案'SharedSizeGroup'
您只需將Grid.IsSharedSizeScope =“True”添加到ItemsControl:
...
</Grid.RowDefinitions>
<ItemsControl Grid.IsSharedSizeScope="True" ItemsSource="{Binding CheckBoxItems}" Grid.Row="0" Grid.Column="0" >
<ItemsControl.ItemsPanel>
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.