2013-04-17 164 views
0

所有,我有一些測試代碼是被定義爲如何將數據綁定到數據網格在運行時

private ObservableCollection<TestClass> _testData = new ObservableCollection<TestClass>(); 
public ObservableCollection<TestClass> TestData 
{ 
    get { return _testData; } 
    set { _testData = value; } 
} 

一些測試數據的工作,並通過

<DataGrid x:Name="dataGrid" ItemsSource="{Binding TestData}".../> 

在設計時,必然我創建了一個DataGrid它獲取在運行時通過

dataGrid.ItemsSource = BuildDataGridColumns(cultureDict).Tables[0].AsDataView(); 

填充

private DataSet BuildDataGridColumns(Dictionary<string, string> cultureDict, 
            DataTable additionalDt = null) 
{ 
    // ... 
} 

這很好用,但更新網格視覺效果的代碼不再適用於測試數據(我相信)ItemsSource="{Binding TestData}"不存在。是應該在DataGrid的細胞插入TextBox和文字結合的XAML是:

<DataGrid x:Name="dataGrid" 
      local:DataGridTextSearch.SearchValue="{Binding ElementName=searchBox, Path=Text, UpdateSourceTrigger=PropertyChanged}" 
      AlternatingRowBackground="Gainsboro" AlternationCount="2" HorizontalAlignment="Stretch" 
      VerticalAlignment="Stretch"> 
    <DataGrid.Resources> 
     <local:SearchValueConverter x:Key="searchValueConverter" /> 
     <Style TargetType="{x:Type DataGridCell}"> 
     <Setter Property="local:DataGridTextSearch.IsTextMatch"> 
      <Setter.Value> 
       <MultiBinding Converter="{StaticResource searchValueConverter}"> 
        <Binding RelativeSource="{RelativeSource Self}" Path="Content.Text" /> 
        <Binding RelativeSource="{RelativeSource Self}" Path="(local:DataGridTextSearch.SearchValue)" /> 
       </MultiBinding> 
      </Setter.Value> 
     </Setter> 
     <Style.Triggers> 
      <Trigger Property="local:DataGridTextSearch.IsTextMatch" Value="True"> 
       <Setter Property="Background" Value="Orange" /> 
      </Trigger> 
     </Style.Triggers> 
     </Style> 
    </DataGrid.Resources> 
    <DataGrid.CellStyle> 
     <Style TargetType="DataGridCell" > 
     <Style.Triggers> 
      <Trigger Property="IsSelected" Value="True"> 
       <Setter Property="Background" Value="#FF007ACC"/> 
       <Setter Property="Foreground" Value="White"/> 
      </Trigger> 
     </Style.Triggers> 
     </Style> 
    </DataGrid.CellStyle> 
</DataGrid> 

我的問題是;

如何創建我的DataGrid與我在運行時創建的數據集相同的綁定?

有人建議,理由是,我的數據未實現INotifyCollectionChanged接口,但由於數據的變動性,我在運行時加載數據到DataGrid

我如何可以綁定這個數據到DataGrid作爲實現INotifyPropertyChanged如果數據結構可以改變一個類?

非常感謝您的時間。

回答

1

我認爲布萊姆是對的。 (對不起,我沒有權限添加評論。)在你的測試代碼中,你使用了一個ObservableCollection,它實現了INotifyPropertyChanged。 DataGrid將選擇對該集合的更改。您應該考慮在實際代碼中使用ObservableCollection,並通過它更新數據,以便通過綁定的DataGrid拾取,而不是直接通過dataGrid.ItemsSource拾取。

此頁面上的答案的方法可以幫助:https://stackoverflow.com/a/1581588/1082026

最終,你的代碼可能看起來像這樣(請注意,你必須定義一個類DataItem的將反映排在你的數據集的數據表,特別是與性能的的DataGrid關心):

private ObservableCollection<DataItem> dataItems= new ObservableCollection<DataItem>(); 
public ObservableCollection<DataItem> DataItems 
{ 
    get { return this.dataItems; } 
    set 
    { 
     this.dataItems = value; 
     base.OnPropertyChanged("DataItems"); 
    } 
} 

有了這個結合

<DataGrid x:Name="dataGrid" ItemsSource="{Binding TestData}".../> 

如果你不想處理add()和刪除(),這將是你如何設置集合:

IList<DataItem> items = BuildDataGridColumns(cultureDict).Tables[0].AsDataView(); 
this.DataItems = new ObservableCollection<DataItem>(items); 

... 

private IList<DataItem> BuildDataGridColumns(Dictionary<string, string> cultureDict, 
            DataTable additionalDt = null) 
{ 
    // ... here create a list of DataItems from your table, and return it. 
} 

希望幫助!

+0

這是一個很好的答案,我認識並開始了昨晚的工作。非常感謝你花時間陪伴... – MoonKnight