2010-01-25 146 views
5

我有一個自定義對象的列表:說果實具有兩個字符串屬性名稱和顏色。這些都在列表中。WPF Datagrid綁定到列表問題

private readonly List<Fruit> fruitList = new List<Fruit>(); 

然後我加載水果對象到列表中。

我想結合這個名單WPF的Datagrid:

C#:

dgFruit.ItemsSource = "{Binding}"; 

XAML:

<toolkit:DataGrid Name="dgFruit" 
      ItemsSource="{Binding Path=fruitList}" > 
       <toolkit:DataGrid.Columns> 

        <toolkit:DataGridComboBoxColumn 
      Header="Name" 
      SelectedValueBinding="{Binding Path=Name}" 
      TextBinding="{Binding Path=Name}" Width="5*" /> 

        <toolkit:DataGridComboBoxColumn 
      Header="Color" 
      SelectedValueBinding="{Binding Path=Color}" 
      TextBinding="{Binding Path=Color}" Width="5*" /> 

       </toolkit:DataGrid.Columns> 
      </toolkit:DataGrid> 

原因,他們是在一個組合框,是因爲我希望用戶成爲能夠改變關係。這不是真正的例子,但你明白了。說爲了這個例子水果不成熟,所以他們改變香蕉的顏色爲綠色:)

我沒有任何運氣在datagrid中獲取這些項目...並沿着軌道,我想要一個點擊該項目在datagridcell更改爲組合框,並顯示所有可能的類型的水果名稱和顏色的(這樣他們就可以改變的關係)

這裏是一個錯誤我越來越:

System.Windows.Data Error: 39 : BindingExpression path error: 'Color' property not found on 'object' ''Char' (HashCode=6750311)'. BindingExpression:Path=Color; DataItem='Char' (HashCode=6750311); target element is 'TextBlockComboBox' (Name=''); target property is 'Text' (type 'String') 

燦有人幫忙嗎?我在xaml中設置我的柱子的原因是我可以將寬度設置爲星號,並且列寬相等。

我看到大多數例子使用ObservableCollection,但如果我可以綁定到列表爲什麼我必須使用它?

請讓我knwo如果我的例子中需要進一步澄清

編輯:我現在有:

XAML:

  <toolkit:DataGrid Name="dgFruit" 
      ItemsSource="{Binding}" 
      AutoGenerateColumns="False"> 

       <toolkit:DataGrid.Columns> 
        <toolkit:DataGridTextColumn 
         Header="Name" 
         Width="5*"/> 

        <toolkit:DataGridComboBoxColumn 
      Header="Color" 
      Width="5*"/> 

        </toolkit:DataGrid.Columns> 
</toolkit:DataGrid> 

C#:

DataContext = fruitList; 

其實當我使用DataContext時,我沒有得到任何迭代女士。當我使用ItemSource時,我得到空行,但正確的行數,所以這看起來更符合正確的線條。

我可以獲取數據以顯示我是否讓它自動生成列。如果我將autogeneratecolumns設置爲false,然後在xaml中指定我的列,就像我想要使用星號選擇大小一樣,那麼我會得到正確數量的列但沒有文本!

回答

4

首先實現INotifyCollectionChangedINotifyPropertyChanged,這樣的:

dgFruit.ItemsSource = "{Binding}" 

應的項目源設置爲包含單詞括號綁定的字符串。這幾乎肯定不是你想要的(事實上,如果你這樣做,你最終應該有一個九行的網格,一個字符串中的每個字符!)。您可以在XAML中設置ItemsSource,也可以在後面設置代碼,但不應該同時執行這兩個操作。請嘗試以下操作:

<toolkit:DataGrid Name="dgFruit"  
        ItemsSource="{Binding}"> 
    ... 
</toolkit:DataGrid> 

,然後在視覺的構造函數:

... 
    InitializeComponents(); 
    this.DataContext = fruitList; 
... 

現在的數據上下文你的整個視覺的列表<水果>,和您的網格的ItemsSource設置爲同一個對象。我在這裏假設fruitList是視覺本身的一個領域。

爲你列的綁定應該是這個樣子:

 <toolkit:DataGridTextColumn Header="Name" 
            Binding="{Binding Name}" 
            Width="5*" /> 
     <toolkit:DataGridComboBoxColumn Header="Color" 
             ItemsSource="{Binding Source={StaticResource AllColors}}" 
             SelectedValueBinding="{Binding Path=Color}" 
             TextBinding="{Binding Path=Color}" 
             Width="5*" /> 

凡AllColors被定義爲資源(​​在這種情況下):

<x:Array x:Key="AllColors" 
     Type="sys:String"> 
    <sys:String>Orange</sys:String> 
    <sys:String>Yellow</sys:String> 
</x:Array> 

這應該讓你開始。

+0

它owrks,但是當我這樣做時,我在xaml中定義的列將被忽略,並在左側添加新的列。我可以以編程方式爲這些自動生成的列設置星形大小嗎? – baron 2010-01-25 02:29:55

+0

您可以關閉自動生成的。在您的DataGrid在XAML: AutoGenerateColumns =「False」 – lesscode 2010-01-25 02:32:02

+0

越來越近,現在沒有任何項目顯示:(儘管最近編輯 – baron 2010-01-25 02:52:15

4

WPF不支持綁定到字段。創建一個訪問fruitList並綁定到該屬性的屬性。

// this is the field. you can't bind to this 
    private readonly List<Fruit> fruitList = new List<Fruit>(); 

    // this is the property. you can bind to this 
    public List<Fruit> FruitList 
    { 
     get { return fruitList; } 
    } 

使用dgFruit.DataContext = this;而不是dgFruit.ItemsSource = "{Binding}";

如果你想在一個組合框,以顯示這些,你需要將這些DataGridComboBoxColumn結合的顏色列表,不只是一個字符串。例如,你的類可能看起來像

public class Fruit 
{ 
    public string Name { get; set; } 
    public string Color { get; set; } // bind the combobox selectedvalue to this 

    public List<string> AvailableColors { get; set; } // bind the combobox ItemsSource to this 
} 

你也可以使用一個List如果你喜歡。的ObservableCollection的優點是它已經爲您

+0

我不明白你的意思是通過創建一個訪問fruitlist的屬性。我想我可以使用ObservableCollection只是我在其他地方使用列表。 而我不知道它需要成爲一個列表。我希望該列表是Fruit.Color或Fruit.Name中的所有值。 gridview shuold顯示原始關係,即蘋果 - >紅色和香蕉 - >黃色是值,但如果我雙擊蘋果,我會看到與水果物體的所有選項組合,所以蘋果和香蕉,以及類似的顏色.. – baron 2010-01-25 02:27:53