如何在wpf數據網格中綁定(Itemssource和選定的項目)組合框?我正在使用MVVM模式。 我試圖如何綁定wpf數據網格中的組合框?
<DataGridTemplateColumn Header="Example 9">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding PartIds, RelativeSource={RelativeSource AncestorType=Window}}" SelectedItem="{Binding PartId, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
和
用於數據綁定#region DataGrid List<String> Example
public ObservableCollection<MyCar> MyCars { get; set; }
public List<string> PartIds { get; set; }
#endregion
#region DataGrid List<Class> Example
public List<CarPart> CarParts { get; set; }
#endregion
參考
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding MyCars}" HorizontalAlignment="Left">
<DataGrid.Columns>
<DataGridTextColumn Header="Model" Binding="{Binding Model}"/>
<DataGridTextColumn Header="Registration" Binding="{Binding Registration}"/>
<DataGridTemplateColumn Header="Example 12">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding CarParts, RelativeSource={RelativeSource AncestorType=Window}}" DisplayMemberPath="PartName" SelectedValuePath="PartID" SelectedValue="{Binding PartId, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Selected" Binding="{Binding PartId}"/>
</DataGrid.Columns>
</DataGrid>
屬性: http://code.msdn.microsoft.com/windowsdesktop/Best-ComboBox-Tutorial-5cc27f82
你如何設置窗口控件的DataContext? –
在代碼文件中。 – user2330678
公共部分類MainWindow:Window { MainWindowViewModel vm = new MainWindowViewModel(); () public MainWindow() this.DataContext = vm; InitializeComponent(); }} – user2330678