繁体   English   中英

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

[英]Some items not showing up in ItemsControl via ItemsSource

我试图获得一个包含网格的ItemsControl,以显示9个具有不同属性的复选框。 但是,只有三个出现。

我有一个复选框模型类,该类具有四个表示复选框内容的属性,grid.row / column和isChecked属性。 然后,我在视图模型中创建其中的九个并将它们添加到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>

看起来是这样的:

在此处输入图片说明

这是我想要的样子:

在此处输入图片说明

问题在于,为ObservableCollection每个项目创建了一个新的Grid,但是您希望所有项目都使用一个Grid。

你可以在设置ItemsPanel中的ItemsControlGrid并使用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>

在此处输入图片说明

我认为您的问题是丢失的物品只是垂直堆叠在视线之外。 您需要为项目面板使用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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM