2011-03-23 112 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

相關問題