首页 文章

某些项目未通过ItemsSource显示在ItemsControl中

提问于
浏览
0

我正在尝试获取包含网格的ItemsControl,以显示具有不同属性的9个复选框 . 然而,只有三个出现过 .

我有一个复选框模型类,它有四个属性,表示复选框的内容,grid.row /列和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>

这是它的样子:

enter image description here

这就是我想要它的样子:

enter image description here

2 回答

  • 1

    问题是为 ObservableCollection 中的每个项目创建了一个新的网格,但是对于所有项目,您需要一个网格 .

    您可以将 ItemsControlItemsPanel 设置为 Grid ,并使用 ItemContainerStyle 设置每个项目容器的 Grid.RowGrid.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>
    

    enter image description here

  • 2

    我认为你的问题是丢失的物品只是垂直堆放在视线之外 . 你想要一个 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>
    

相关问题