C# MVVM中的DataGridComboxColumn SelectionChanged事件
我有一个DataGrid,它包含一个DataGridComboxColumn。我想做的是根据这个组合框的值(dis)启用其他列。为此,我通过ICommand处理SelectionChanged事件,但从未调用此命令。对于DataGrid之外的组合框来说,它工作得很好,但是DataGrid内部的诀窍是什么呢 首先,选项的值仅在行不再处于编辑模式时设置 第二句话,当按enter键结束编辑时,即使使用组合框更改了选项,也不会设置选项C# MVVM中的DataGridComboxColumn SelectionChanged事件,c#,wpf,mvvm,datagrid,C#,Wpf,Mvvm,Datagrid,我有一个DataGrid,它包含一个DataGridComboxColumn。我想做的是根据这个组合框的值(dis)启用其他列。为此,我通过ICommand处理SelectionChanged事件,但从未调用此命令。对于DataGrid之外的组合框来说,它工作得很好,但是DataGrid内部的诀窍是什么呢 首先,选项的值仅在行不再处于编辑模式时设置 第二句话,当按enter键结束编辑时,即使使用组合框更改了选项,也不会设置选项 <DataGrid Grid.Row="0" AutoGen
<DataGrid Grid.Row="0" AutoGenerateColumns="False"
ItemsSource="{Binding Path=AcquisitionList, Mode=TwoWay}"
SelectedItem="{Binding SelectedParameters}"
Margin="0,20,0,0" Name="dataGridAcquisitions"
CanUserAddRows="True" CanUserDeleteRows="True"
CanUserReorderColumns="False" SelectionUnit="FullRow">
<DataGridComboBoxColumn Header="Choice1" Width="SizeToHeader"
SelectedItemBinding="{Binding Options}"
ItemsSource="{Binding Source={StaticResource OptionValues}}"
EditingElementStyle="{StaticResource StandardComboBox}"
ElementStyle="{StaticResource StandardComboBox}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding EnableCustomParameters}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGridComboBoxColumn>
<DataGridComboBoxColumn Header="Choice2" Width="SizeToHeader"
SelectedItemBinding="{Binding FilterType}"
ItemsSource="{Binding Source={StaticResource FilterTypes}}"
EditingElementStyle="{StaticResource StandardComboBox}"
ElementStyle="{StaticResource StandardComboBox}" >
<DataGridComboBoxColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="IsEnabled" Value="{Binding CustomParametersEnabled}"/>
</Style>
</DataGridComboBoxColumn.CellStyle>
</DataGridComboBoxColumn>
</DataGrid>
您应该使用适当的绑定:
{Binding Path=yourCommandName,RelativeSource={RelativeSource-AncestorType={x:Type-DataGrid}}}
从中了解更多有关不同绑定的信息。这不是一个真正的解决方案,而是一种变通方法。由于我没有找到在combobox项更改时触发事件的方法,因此我使用了属性选项 首先,我必须使用属性“UpdateSourceTrigger=PropertyChanged”来修饰SelectedItemBinding,该属性指定要更新的绑定源的时间。在doc中,PropertyChanged“只要绑定目标属性发生更改,就会立即更新绑定源。”这正是我要寻找的行为 然后,对模型进行如下修改:
public class AcquisitionParameters : INotifyPropertyChanged
{
private Choice1 options;
private Choice2 filterType;
private bool customParametersEnabled;
public Choice1 Options
{
get { return options; }
set
{
options= value;
CustomParametersEnabled = options == Choice1.Options1;
OnPropertyChanged("Options");
OnPropertyChanged("CustomParametersEnabled ");
}
}
public Choice2 FilterType
{
get { return filterType; }
set { filterType= value; OnPropertyChanged("FilterType"); }
}
public bool CustomParametersEnabled
{
get { return customParametersEnabled; }
set { customParametersEnabled = value; OnPropertyChanged("CustomParametersEnabled"); }
}
}
然后,当参数选项的第一个组合框的所选项目发生更改时,将立即设置属性选项,并在其中更新CustomParametersEnabled。这不是我第一次尝试时想要的方式,但它很有效。谢谢你的回复,但它也不起作用。我以前尝试过这个解决方案,但没有成功。我试图绑定到DataGrid或UserControl祖先,但失败了。我对Choice1组合框的ItemsSource也有同样的问题。为此,解决方法是创建一个静态资源,因为选项列表不会更改,并且是控件的属性
public class AcquisitionParameters : INotifyPropertyChanged
{
private Choice1 options;
private Choice2 filterType;
private bool customParametersEnabled;
public Choice1 Options
{
get { return options; }
set { options= value; OnPropertyChanged("Options"); }
}
public Choice2 FilterType
{
get { return filterType; }
set { filterType= value; OnPropertyChanged("FilterType"); }
}
public bool CustomParametersEnabled
{
get { return customParametersEnabled; }
set { customParametersEnabled = value; OnPropertyChanged("CustomParametersEnabled"); }
}
}
public class AcquisitionParameters : INotifyPropertyChanged
{
private Choice1 options;
private Choice2 filterType;
private bool customParametersEnabled;
public Choice1 Options
{
get { return options; }
set
{
options= value;
CustomParametersEnabled = options == Choice1.Options1;
OnPropertyChanged("Options");
OnPropertyChanged("CustomParametersEnabled ");
}
}
public Choice2 FilterType
{
get { return filterType; }
set { filterType= value; OnPropertyChanged("FilterType"); }
}
public bool CustomParametersEnabled
{
get { return customParametersEnabled; }
set { customParametersEnabled = value; OnPropertyChanged("CustomParametersEnabled"); }
}
}