我试图获得一个包含网格的ItemsControl,以显示具有不同属性的9个复选框。然而,只有三个出现过。
我有一个复选框模型类,它有四个属性代表复选框' content,grid.row / column和isChecked属性。然后我在我的viewmodel中创建其中的九个并将它们添加到ObservableCollection
。
接下来,我将ItemsControl的ItemsSource绑定到集合。然后,我将一个复选框控件添加到ItemsControl内部的网格中,并绑定相应的属性。
但是,只显示前三个复选框,我不明白为什么。我通过调试验证了绑定的集合确实具有正确数量的项目。
这是我的CheckboxModel类:
public class CheckboxModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool _isChecked;
public bool IsChecked
{
get { return _isChecked; }
set { _isChecked = value; OnPropertyChanged("IsChecked"); }
}
private string _content;
public string Content
{
get { return _content; }
set { _content = value; OnPropertyChanged("Content"); }
}
private int _gridRow;
public int GridRow
{
get { return _gridRow; }
set { _gridRow = value; OnPropertyChanged("GridRow"); }
}
private int _gridColumn;
public int GridColumn
{
get { return _gridColumn; }
set { _gridColumn = value; OnPropertyChanged("GridColumn"); }
}
// Create the OnPropertyChanged method to raise the event
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
在我的视图模型中,我创建了一个这样的集合:
private ObservableCollection<CheckboxModel> _checkBoxList = new ObservableCollection<CheckboxModel>();
public ObservableCollection<CheckboxModel> CheckBoxList
{
get
{
return _checkBoxList;
}
set
{
if (null != value)
{
_checkBoxList = value;
OnPropertyChanged("CheckBoxList");
}
}
}
public void CreateCheckboxList()
{
CheckBoxList.Add(new CheckboxModel { IsChecked = true, Content = "Latitude", GridRow = 0, GridColumn = 0 });
CheckBoxList.Add(new CheckboxModel { IsChecked = true, Content = "Longitude", GridRow = 1, GridColumn = 0 });
CheckBoxList.Add(new CheckboxModel { IsChecked = true, Content = "Altitude", GridRow = 2, GridColumn = 0 });
CheckBoxList.Add(new CheckboxModel { IsChecked = true, Content = "Depth", GridRow = 0, GridColumn = 1 });
CheckBoxList.Add(new CheckboxModel { IsChecked = true, Content = "Speed", GridRow = 1, GridColumn = 1 });
CheckBoxList.Add(new CheckboxModel { IsChecked = true, Content = "Heading", GridRow = 2, GridColumn = 1 });
CheckBoxList.Add(new CheckboxModel { IsChecked = true, Content = "Roll", GridRow = 0, GridColumn = 2 });
CheckBoxList.Add(new CheckboxModel { IsChecked = true, Content = "Pitch", GridRow = 1, GridColumn = 2 });
CheckBoxList.Add(new CheckboxModel { IsChecked = true, Content = "VOS", GridRow = 2, GridColumn = 2 });
}
然后终于来了我的xaml:
<ItemsControl ItemsSource="{Binding CheckBoxList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="149.6*" />
<ColumnDefinition Width="149.6*" />
<ColumnDefinition Width="149.6*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="28*" />
<RowDefinition Height="28*" />
<RowDefinition Height="28*" />
</Grid.RowDefinitions>
<CheckBox Grid.Row="{Binding GridRow}"
Grid.Column="{Binding GridColumn}"
Margin="14,6,63,6"
VerticalAlignment="Center"
Content="{Binding Content}"
IsChecked="{Binding IsChecked}" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
这就是它的样子:
这就是我想要的样子:
答案 0 :(得分:2)
问题在于为ObservableCollection
中的每个项目创建了一个新的网格,但是您想要为所有项目使用单个网格。
您可以将ItemsPanel
的{{1}}设置为ItemsControl
并使用Grid
设置ItemContainerStyle
和Grid.Row
附加属性每个项目容器。
这应该有效:
Grid.Column
答案 1 :(得分:1)
我认为你的问题是丢失的物品只是垂直堆放在视线之外。您想要物品面板WrapPanel
。您还需要确保ItemsControl不会自动调整其大小比其父级大。
<ItemsControl
VerticalAlignment="Stretch"
ItemsSource="{Binding CheckBoxList}">
<!-- Add this-->
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel
Orientation="Vertical"
/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<!-- Done -->
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="149.6*" />
<ColumnDefinition Width="149.6*" />
<ColumnDefinition Width="149.6*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="28*" />
<RowDefinition Height="28*" />
<RowDefinition Height="28*" />
</Grid.RowDefinitions>
<CheckBox Grid.Row="{Binding GridRow}"
Grid.Column="{Binding GridColumn}"
Margin="14,6,63,6"
VerticalAlignment="Center"
Content="{Binding Content}"
IsChecked="{Binding IsChecked}"
/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>