无法创建动态网格

时间:2015-03-03 09:53:21

标签: c# wpf mvvm

我是WPF MVVM的新手我正在创建动态grid.my代码如下

  public class Property : INotifyPropertyChanged
{
    public Property(string name, object value)
    {
        Name = name;
        Value = value;
    }

    public string Name { get; private set; }
    public object Value { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;
}
public class Record
{
    private readonly ObservableCollection<Property> properties = new ObservableCollection<Property>();

    public Record(params Property[] properties)
    {
        foreach (var property in properties)
            Properties.Add(property);
    }

    public ObservableCollection<Property> Properties
    {
        get { return properties; }
    }
}

public class DataGridColumnsBehavior
{

    public static readonly DependencyProperty BindableColumnsProperty =
        DependencyProperty.RegisterAttached("BindableColumns",
        typeof(ObservableCollection<DataGridColumn>),
        typeof(DataGridColumnsBehavior),
        new UIPropertyMetadata(null, BindableColumnsPropertyChanged));

    private static void BindableColumnsPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
    {
        DataGrid myDataGrid = source as DataGrid;
        ObservableCollection<DataGridColumn> columns = e.NewValue as ObservableCollection<DataGridColumn>;

        myDataGrid.Columns.Clear();
        if (columns == null)
            return;

        foreach (DataGridColumn column in columns)
        {
            myDataGrid.Columns.Add(column);
        }

        columns.CollectionChanged += (sender, e1) =>
        {

       -----------// Others stuff
}

    private void FillData()
    {
        List<DataClass> MyClass = new List<DataClass>();
        records = new ObservableCollection<Record>();
        records.Add(new Record(new Property("FirstName", "Name1"), new Property("LastName", "Name2")));
        records.Add(new Record(new Property("FirstName", "Name3"), new Property("LastName", "Name4")));

        var columns = records.First()
                     .Properties
                     .Select((x, i) => new { Name = x.Name, Index = i })
                     .ToArray();

        ColumnCollection = new ObservableCollection<DataGridColumn>();
        foreach (var column in columns)
        {
            var binding = new Binding(string.Format("Properties[{0}].Value", column.Index));

            ColumnCollection.Add(new DataGridTextColumn() { Header = column.Name, Binding = binding });
        }
        MyClass.Add(new DataClass() { HeaderName = "HeaderVal", GridVal = records });
        myListBox.ItemsSource = MyClass;
    }

在我的MainWindow中我有

       <Grid>
    <ListBox Width="400" Margin="10"  x:Name="myListBox">
        <ListBox.ItemTemplate>
        <DataTemplate>
                <Expander Header="Header1" IsExpanded="False">
                    <StackPanel>
                        <DataGrid
                             x:Name="dataGrid"
                             local:DataGridColumnsBehavior.BindableColumns="{Binding ColumnCollection}"
                             AutoGenerateColumns="False"
                             ItemsSource="{Binding Path=records}"/>
                    </StackPanel>
                </Expander>
        </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

但网格不可见。我错过了什么?

1 个答案:

答案 0 :(得分:2)

您正在通过

创建一个DataClass实例
new DataClass() { HeaderName = "HeaderVal", GridVal = records }

这意味着GridVal属性可以访问“记录”。因此你的绑定应该是

ItemsSource="{Binding Path=GridVal}"

此外,ColumnCollection也应该是DataClass类中的属性,以使此绑定起作用:

local:DataGridColumnsBehavior.BindableColumns="{Binding ColumnCollection}"

否则,您必须明确指定该绑定的SourceRelativeSource

您应该在Visual Studio的“输出窗口”中观察到绑定错误消息。

您的Property类实现了INotifyPropertyChanged,但从未引发过PropertyChanged事件,这看起来也很可疑。