某些项目未通过 ItemsSource 显示在 ItemsControl 中
Some items not showing up in ItemsControl via ItemsSource
我正在尝试获取一个包含网格的 ItemsControl,以显示 9 个具有不同属性的复选框。然而,只有三个人出现。
我有一个复选框模型 class,它有四个属性代表复选框的内容、grid.row/column 和 isChecked 属性。然后我在我的视图模型中创建了其中的九个并将它们添加到 ObservableCollection
.
接下来我将 ItemsControl 的 ItemsSource 绑定到集合。然后,我将复选框控件添加到 ItemsControl 内的网格并绑定适当的属性。
但是,只有前三个复选框出现了,我不知道为什么。我确实通过调试验证了绑定到的集合确实具有正确数量的项目。
这是我的 CheckboxModel class:
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>
这是它的样子:
这是我想要的样子:
我认为您的问题是丢失的物品只是垂直堆叠在看不见的地方。您想要一个 WrapPanel
作为您的项目面板。您还需要确保您的 ItemsControl 本身 auto-sizing 不大于其父项。
<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>
问题是为您 ObservableCollection
中的每个项目创建了一个新网格,但您希望所有项目都使用一个网格。
您可以将 ItemsControl
的 ItemsPanel
设置为您的 Grid
并使用 ItemContainerStyle
设置 Grid.Row
和 Grid.Column
每个项目容器的附加属性。
这应该有效:
<ItemsControl ItemsSource="{Binding CheckBoxList}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<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>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Grid.Row" Value="{Binding GridRow}" />
<Setter Property="Grid.Column" Value="{Binding GridColumn}" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox Margin="14,6,63,6"
VerticalAlignment="Center"
Content="{Binding Content}"
IsChecked="{Binding IsChecked}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我正在尝试获取一个包含网格的 ItemsControl,以显示 9 个具有不同属性的复选框。然而,只有三个人出现。
我有一个复选框模型 class,它有四个属性代表复选框的内容、grid.row/column 和 isChecked 属性。然后我在我的视图模型中创建了其中的九个并将它们添加到 ObservableCollection
.
接下来我将 ItemsControl 的 ItemsSource 绑定到集合。然后,我将复选框控件添加到 ItemsControl 内的网格并绑定适当的属性。
但是,只有前三个复选框出现了,我不知道为什么。我确实通过调试验证了绑定到的集合确实具有正确数量的项目。
这是我的 CheckboxModel class:
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>
这是它的样子:
这是我想要的样子:
我认为您的问题是丢失的物品只是垂直堆叠在看不见的地方。您想要一个 WrapPanel
作为您的项目面板。您还需要确保您的 ItemsControl 本身 auto-sizing 不大于其父项。
<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>
问题是为您 ObservableCollection
中的每个项目创建了一个新网格,但您希望所有项目都使用一个网格。
您可以将 ItemsControl
的 ItemsPanel
设置为您的 Grid
并使用 ItemContainerStyle
设置 Grid.Row
和 Grid.Column
每个项目容器的附加属性。
这应该有效:
<ItemsControl ItemsSource="{Binding CheckBoxList}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<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>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Grid.Row" Value="{Binding GridRow}" />
<Setter Property="Grid.Column" Value="{Binding GridColumn}" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox Margin="14,6,63,6"
VerticalAlignment="Center"
Content="{Binding Content}"
IsChecked="{Binding IsChecked}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>