2011-05-12 24 views
1

いくつかのデータを可変数の列を持つDataGridにバインドする必要があります。私はそれが次のコードを使用して働かせた:WPF:動的列で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"の記事を見つけました。しかし、私の場合にはうまく動作するように苦労しました(具体的には、ヘッダバインディングの作業を行うことはできません)。それが機能したとしても、私はまだ一貫性のないセルスタイルのような問題に直面しています。だから私は上記のコードを少しでも微調整してもいいのだろうかと思っています。

EDIT:私の問題に関連する別の投稿を見つけました:Implicit Two Way binding

"双方向バインディングはパスまたはXPathが必要です"というエラーが表示されます。しかし、問題は簡単に私の問題は、同様の方法で解決できるのであれば誰も私にヒントを与えることができ

<TextBox Text="{Binding Path=DataContext, RelativeSource={RelativeSource Self}}"/> 

または

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

を用いて固定することができますか?

+0

私は私の答えを編集する問題は、あなたのコレクションタイプの倍です。 – blindmeis

答えて

2

インデクサーにバインドしますか? DataListプロパティの外観を教えてください。

インデックス済みのプロパティで前と同じことをしました。

public SomeObjectWithIndexer DataList 
{get; set;} 


public class SomeObjectWithIndexer 
{ 
     public string this 
     { 
      get { ... } 
      set { ... }//<-- you need this one for TwoWay 
     } 
} 

EDIT:プロパティを編集できない理由は、「ダブルフィールド」を編集しようとしていることです。 1つの回避策は、INotifyPropertyChangedを持つクラスにダブルをラップすることです。

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:作業例:二重のためだけの双方向を表示するには、

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; } 
} 

この例で正しい方向に進むことを希望します。

+0

@blindmeis:答えをありがとう。私のDataListはObservableCollection として宣言されています。 – newman

+0

ObservableCollectionのインデックスは常に同じですか? – blindmeis

+0

@blindmeis:INotifyChangedをimplemnetedにしてdoubleをオブジェクトに変換してあなたの提案を試みましたが、同じエラーが発生しました。だから、私はそれがインデクサーの問題かどうか分からない。 – newman

関連する問題