将CustomClass的ObservableCollection绑定到Grid

时间:2014-03-13 10:25:41

标签: c# wpf observablecollection uniformgrid

我必须在按钮点击的网格中显示Observable Collection的内容。单击按钮(我知道可以使用Command绑定,但为了简单起见,单击处理程序)集合应填充并显示模型类的名称属性。

视图模型:

public class Sample
    {
        public ObservableCollection<SchoolModel> ModelCollection { get; set; }

        public void GridMethod()
        {
            ModelCollection = new ObservableCollection<SchoolModel>();
            ModelCollection.Add(new SchoolModel() {Id=1, Name="ABC" });
            ModelCollection.Add(new SchoolModel() { Id = 2, Name = "PQR" });
            ModelCollection.Add(new SchoolModel() { Id = 3, Name = "DEF" });
        }
    }

型号:

public class SchoolModel : INotifyPropertyChanged
    {
        private int id;
        private string name;

        public int Id
        { 
            get 
            {
                return id;
            }
            set 
            {
                id = value;
                OnPropertyChanged("Id");
            }
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
                OnPropertyChanged("Name");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }   
    }

查看:

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <Button Content="Click" Click="Button_Click" Grid.Row="0" Grid.Column="0"/>

        <ItemsControl ItemsSource="{Binding ModelCollection}"  Grid.Row="0" Grid.Column="1" >
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid Rows="{Binding Path=ModelCollection.Count}" Columns="{Binding Path=ModelCollection.Count}" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate >
                    <TextBlock Width="auto" Height="auto" Text="{Binding Path=Name}"/>                    
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>

有什么想法吗?为什么Collection不会显示在Grid中

1 个答案:

答案 0 :(得分:0)

在Sample类的构造函数中初始化集合,否则绑定将在加载时失败,并且在手动为ModelCollection引发PropertyChanged事件之前它将不起作用。

或者:

public Sample()
{
   ModelCollection = new ObservableCollection<SchoolModel>();
}

在Sample类上实现INPC并在PropertyChanged中的初始化逻辑之后引发GridMethod()事件。

    public void GridMethod()
    {
        ModelCollection = new ObservableCollection<SchoolModel>();
        ModelCollection.Add(new SchoolModel() {Id=1, Name="ABC" });
        ModelCollection.Add(new SchoolModel() { Id = 2, Name = "PQR" });
        ModelCollection.Add(new SchoolModel() { Id = 3, Name = "DEF" });
        OnPropertyChanged("ModelCollection");
    }