WPF:如何使动态列的DataGrid绑定可编辑?

时间:2011-05-12 03:25:16

标签: wpf binding datagrid two-way

我需要将一些数据绑定到具有可变列数的DataGrid。我使用以下代码使其工作:

int n = 0;
foreach (string title in TitleList)
{
    DataGridTextColumn col = new DataGridTextColumn();
    col.Header = title;
    Binding binding = new Binding(string.Format("DataList[{0}]", n++));
    binding.Mode = BindingMode.TwoWay;
    col.Binding = binding;
    grid.Columns.Add(col);
}

其中DataList声明为:

public ObservableCollection<double> DataList { get; set; }

和TitleList声明为:

public ObservableCollection<string> TitleList { get; set; }

问题是,即使我指定了TwoWay绑定,它实际上是单向的。当我单击一个单元格以尝试编辑时,我得到一个异常“此视图不允许使用''EditItem'”。我只是错过了绑定表达式中的某些内容吗?

P.S。我从Deborah "Populating a DataGrid with Dynamic Columns in a Silverlight Application using MVVM"找到了一篇文章。但是,我很难让它适用于我的情况(具体来说,我不能使标题绑定工作)。即使它有效,我仍然面临着不一致的细胞样式等问题。这就是为什么我想知道我是否可以使我的上述代码工作 - 稍微调整一下?

编辑:我发现了另一篇可能与我的问题相关的帖子:Implicit Two Way binding。它看起来是否使用

绑定到TextBox的字符串列表
<TextBox Text="{Binding}"/>

您将收到类似“双向绑定需要Path或XPath”的错误。但是使用

可以很容易地解决问题
<TextBox Text="{Binding Path=DataContext, RelativeSource={RelativeSource Self}}"/>

<TextBox Text="{Binding .}"/>

如果我的问题可以用类似的方式解决,有人可以给我一个提示吗?

1 个答案:

答案 0 :(得分:2)

你绑定到索引器吗?你能告诉我们你的DataList属性是什么样的吗?

我前一段时间用索引属性做了同样的事。

 public SomeObjectWithIndexer DataList
 {get; set;}


 public class SomeObjectWithIndexer 
 {
      public string this
      {
          get { ... }
          set { ... }//<-- you need this one for TwoWay
      }
 }
编辑:您无法编辑属性的原因是您尝试编辑“双字段”。 一个解决方法是使用INotifyPropertyChanged将您的double包装到一个类中。

public class DataListItem
{
    public double MyValue { get; set;}//with OnPropertyChanged() and stuff
}

然后你可以使用

ObservableCollection<DataListItem>

你可以编辑你的价值。关于指数始终保持不变的问题仍然存在:)

Binding binding = new Binding(string.Format("DataList[{0}].MyValue", n++));

EDIT2:工作示例:只是为了显示twoway正在运作

public class DataItem
{
    public string Name { get; set; }
    public ObservableCollection<DataListItem> DataList { get; set; }

    public DataItem()
    {
        this.DataList = new ObservableCollection<DataListItem>();
    }
}

双人包装:

public class DataListItem
{
    private double myValue;
    public double MyValue
    {
        get { return myValue; }
        set { myValue = value; }//<-- set breakpoint here to see that edit is working
    }
}

带有数据网格的用户控件

<UserControl x:Class="WpfStackoverflow.IndexCollectionDataGrid"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<DataGrid ItemsSource="{Binding MyList}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" />
        <DataGridTextColumn Header="Index1" Binding="{Binding Path=DataList[0].MyValue, Mode=TwoWay}" />
        <DataGridTextColumn Header="Index2" Binding="{Binding Path=DataList[1].MyValue, Mode=TwoWay}" />
    </DataGrid.Columns>
</DataGrid>
</UserControl>

的.cs

public partial class IndexCollectionDataGrid : UserControl
{
    public IndexCollectionDataGrid()
    {
        InitializeComponent();
        this.MyList = new ObservableCollection<DataItem>();

        var m1 = new DataItem() {Name = "test1"};
        m1.DataList.Add(new DataListItem() { MyValue = 10 });
        m1.DataList.Add(new DataListItem() { MyValue = 20 });

        var m2 = new DataItem() { Name = "test2" };
        m2.DataList.Add(new DataListItem() { MyValue = 100 });
        m2.DataList.Add(new DataListItem() { MyValue = 200 });

        this.MyList.Add(m1);
        this.MyList.Add(m2);

        this.DataContext = this;
    }

    public ObservableCollection<DataItem> MyList { get; set; }
}

我希望你能通过这个例子找到正确的方向。

相关问题