2011-03-23 78 views
1

我知道这个话题已经出现了几次,但我已经尝试了我在这里找到的方式,似乎都没有工作。我不知道是否因为我使用不同的绑定源,或者如果我只是失败,或者什么...WPF ComboBox在DataGridTemplateColumn与备用ItemsSource

我有一个DataGrid绑定到一个XML文档读入内存。我有一个列表,其中包含一列可能会出现的所有不同值,并且希望将其用作ComboBox列的ItemsSource。

我的XAML如下:

<DataGrid AutoGenerateColumns="False" IsReadOnly="False" Height="400" HorizontalAlignment="Left" Margin="125,15,0,0" x:Name="dg" VerticalAlignment="Top" Width="500"> 
    <DataGrid.Columns> 
     <DataGridTextColumn Header="Name" Width="40*" Binding="{Binding Path=Element[name].Value}" /> 
     <DataGridTextColumn Header="Count" Width="10*" Binding="{Binding Path=Attribute[count].Value}" /> 

<!-- I want this to be a ComboBox in a DataGridTemplateColumn --> 
     <DataGridTextColumn Header="Category" Width="25*" Binding="{Binding Path=Attribute[category].Value}" /> 

     <DataGridTextColumn Header="Image Path" Width="25*" Binding="{Binding Path=Element[image].Value}" /> 
    </DataGrid.Columns> 
</DataGrid> 

和对显示的示例XML节点应该是这样的:

<entry count="1" category="someCategory"> 
    <name>Entry 1</name> 
    <image>c:\image.png</image> 
</entry> 

最后,我想作为的ItemsSource为使用的列表组合框:

var categories = from category in xmlDoc.Root.Elements("entry") select category .Attribute("category").Value; 
List<string> catList= categories .ToList<string>(); 

所以,当用户编辑类别字段,我希望他们有一个下拉续确定列表中包含的可能值。


编辑:终于得到了这个工作,我也作为公认的答案表示,设置ComboBox的的ItemsSource到

ItemsSource="{DynamicResource categoryList}" 

,然后只需在代码中这样做创建列表项后我想用填充组合框:

Resources["categoryList"] = catList; 

回答

0

,你必须建立一个CellTemplate和CellEditingTemplate一个DataGridTemplateColumn。下面应该给你正确的方向开始

<DataGridTemplateColumn Header="Category" Width="100"> 
    <DataGridTemplateColumn.CellTemplate> 
    <DataTemplate> 
     <TextBlock Text="{Binding YourProperty}" "/> 
    </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 
    <DataGridTemplateColumn.CellEditingTemplate> 
    <DataTemplate> 
     <ComboBox ItemsSource="{Binding YourSource, Mode=OneTime or OneWay}" 
     </ComboBox> 
    </DataTemplate> 
    </DataGridTemplateColumn.CellEditingTemplate>   
</DataGridTemplateColumn> 
+0

谢谢,这让我走上了正确的轨道......我会发布最终作为编辑工作。 – tanGee 2011-03-23 17:00:53

相关问题