[英]Programatically Binding the value of a ComboBox in a Datagrid based on another Combobox
我有一個綁定到可觀察集合的 DataGrid。 在這個 DataGrid 中,我有兩個組合框(在其他列中)。 其中一列是任務,第二列是進度。 我想要實現的是第二個下拉列表(進度)的項目源,將根據第一個下拉列表(任務)的值進行更新。 這是我到目前為止所做的:
<Window.Resources>
<ObjectDataProvider x:Key="EngTaskEnum"
MethodName="GetValues"
ObjectType="{x:Type core:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type Type="Sources:EngTask"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<DataGrid x:Name="DataGrid_tbl"
ItemsSource="{Binding}"
AutoGenerateColumns="False"
AlternatingRowBackground="LightBlue"
CanUserAddRows="True"
CanUserResizeColumns="False"
CanUserReorderColumns="False"
CanUserResizeRows="False"
CanUserSortColumns="False"
SelectionMode="Single"
CellEditEnding="DataGrid_tbl_CellEditEnding"
PreparingCellForEdit="DataGrid_tbl_PreparingCellForEdit">
<DataGrid.Columns>
<!--Task-->
<DataGridComboBoxColumn SelectedItemBinding="{Binding EngTask}"
ItemsSource="{Binding Source={StaticResource EngTaskEnum}}"
Header="Task"
Width="Auto"
MinWidth="120"/>
上面顯示的是(任務)列,進度列遵循相同的模式。 這就是我在代碼隱藏上嘗試的:
private void DataGrid_tbl_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
{
if(e.Column.Header.ToString() == "Progress")
{
switch (_taskSelected)
{
case"Software":
e.EditingEventArgs.Source = Enum.GetValues(typeof(SoftProgress));
break;
case "Hardware":
e.EditingEventArgs.Source = Enum.GetValues(typeof(HardProgress));
break;
}
}
}
我幾乎可以肯定,這很可能不是正確的方法,但我一直在嘗試找出不同的方法,只是為了看看哪些方法可行。 請如果有人可以提供一些輸入將不勝感激。 謝謝!
您應該將第二個ComboBox
綁定到定義了EngTask
屬性的 class 的IEnumerable
屬性。
當設置EngTask
時,當您 select 在第一個ComboBox
中設置一個值時,您將IEnumerable
屬性設置為要在第二個ComboBox
中顯示的項目。
確保實現INotifyPropertyChanged
接口並在 model class(定義EngTask
)中為IEnumerable
屬性引發PropertyChanged
事件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.