2016-10-21 143 views
2

我想構建一個動態生成列的DataGrid(這工作正常),但我無法爲自動更新的列(如INotifyPropertyChanged)創建綁定。無法填充DataGrid

動態創建列並希望使用字典元素進行綁定,可以動態修改/添加。在Visual Studio的調試輸出中沒有看到任何錯誤。

我想我真的很想念這裏的小事。

點擊按鈕不填充第二列

clicking button does not populate anything

視圖模型:

class DataGridAttachedPropertyViewModel { 


    public ObservableCollection<DataGridColumn> ColumnCollection { get; set; } 
    public ObservableCollection<AttachedPropertyEmployee> SomEmployees { get; set; } 

    public ICommand myCommand { get; set; } 
    public DataGridAttachedPropertyViewModel() { 

     this.ColumnCollection = new ObservableCollection<DataGridColumn>(); 


     DataGridTextColumn tc = new DataGridTextColumn(); 
     tc.Header = "Sample Column"; 
     // tc.Binding = new Binding("name"); 
     Binding forCurrent = new Binding("SimpleDict[f]"); 
     forCurrent.Mode = BindingMode.TwoWay; 
     tc.Binding = forCurrent; 

     DataGridTextColumn tt = new DataGridTextColumn(); 
     tt.Header = "Column x"; 
     // tc.Binding = new Binding("name"); 
     Binding forTheCurrent = new Binding("SimpleDict[x]"); 
     forTheCurrent.Mode = BindingMode.TwoWay; 

     tt.Binding = forTheCurrent; 

     myCommand = new DelegateCommand(ButtonBase_OnClick); 

     this.ColumnCollection.Add(tc); 
     this.SomEmployees = new ObservableCollection<AttachedPropertyEmployee>(); 
     this.SomEmployees.Add(new AttachedPropertyEmployee("Rajat","Norwalk")); 

     this.SomEmployees.Add(new AttachedPropertyEmployee("Matthew", "Norwalk")); 
    } 

    public void ButtonBase_OnClick() { 
     foreach (var VARIABLE in SomEmployees) { 
      VARIABLE.SimpleDict["x"] = "x"; 
     } 
    } 

} 

AttachedPropertyEmployee.cs

public class AttachedPropertyEmployee : INotifyPropertyChanged { 

    private Dictionary<string, string> dict; 

    public Dictionary<string, string> SimpleDict { 
     get { return this.dict; } 
     set 
     { 
      if (this.dict != value) { 
       this.dict = value; 
       this.NotifyPropertyChanged("SimpleDict"); 
      } 
     } 
    } 

    public AttachedPropertyEmployee(string Name, string Address) { 
     this.SimpleDict = new Dictionary<string, string>(); 
     SimpleDict["f"] ="b"; 
     this.name = Name; 
     this.address = Address; 
    } 

    public string name; 
    public string address { get; set; } 


    public event PropertyChangedEventHandler PropertyChanged; 

    public void NotifyPropertyChanged(string propName) { 
     if (this.PropertyChanged != null) 
      this.PropertyChanged(this, new PropertyChangedEventArgs(propName)); 
    } 
} 

XAML:

<Window x:Class="LearnInteractivity.LearnDataGridAttachedProperty" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:LearnInteractivity" 
    mc:Ignorable="d" 
    Title="LearnDataGridAttachedProperty" Height="300" Width="300"> 

<!-- 
Put a datargrid and an attached property and update columns dynamincally. 

--> 


<StackPanel> 

    <DataGrid 
local:DataGridColumnsBehavior.BindableColumns="{Binding ColumnCollection}" 
     x:Name="dgg" 
     AutoGenerateColumns="False" 
     ItemsSource="{Binding SomEmployees}"></DataGrid> 
    <Button Content="Populate" Command="{Binding myCommand}"></Button> 

</StackPanel> 

+2

你設置了'VARIABLE.SimpleDict [「x」] =「x」;',但你不告訴任何人。當你這樣做的時候沒有事件發生。 'Dictionary'不實現'INotifyCollectionChanged'。 –

+0

謝謝!那麼,可以做些什麼改變。請爲我的目標建議一些可以使用的DS /其他解決方法:我希望能夠動態添加列,並相應地映射到某些詞典,如可以添加/修改內容的結構。 – 10101010

+1

嘗試'VARIABLE.NotifyPropertyChanged(「SimpleDict」)'當您更改其中的值時。 –

回答

1

我看到這裏有兩個問題。

第一個是Dictionary<TKey,TValue>沒有實現INotifyCollectionChanged,所以當你改變它的值時,不會引發任何事件,UI也不會知道它。你可以尋找一個ObservableDictionary<K,V>和使用(IIRC周圍有幾個實現),或者你可以做它的快速和骯髒的方式:

public void ButtonBase_OnClick() { 
    foreach (var VARIABLE in SomEmployees) { 
     VARIABLE.SimpleDict["x"] = "x"; 
     VARIABLE.NotifyPropertyChanged("SimpleDict"); 
    } 
} 

這將通知網格SimpleDict已經改變。

第二個問題是,在DataGridAttachedPropertyViewModel的構造函數中,您忘記將tt添加到ColumnCollection

this.ColumnCollection.Add(tc); 
    this.ColumnCollection.Add(tt); 

更多的想法:

我會更舒服加入這樣的事情AttachedPropertyEmployee

public void SetColumValue(string key, string value) { 
    SimpleDict[key] = value; 
    NotifyPropertyChanged("SimpleDict"); 
} 

和使用,在你的循環,而不是:

public void ButtonBase_OnClick() { 
    foreach (var VARIABLE in SomEmployees) { 
     VARIABLE.SetColumnValue("x", "x"); 
    } 
} 

順便說一句,我會改變SimpleDictDictionary<String, Object>所以你c支持更多類型而不僅僅是字符串,並將格式留給UI。我可能會考慮將SimpleDict屬性中的ReadOnlyDictionary<K,V>公開,並且可寫字典是專用字段 - 因此,呼叫者將別無選擇,只能使用SetColumnValue(k,v)來設置列值。