2010-01-25 148 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