gpt4 book ai didi

c# - MVVM 中的 DataGridComboBoxColumn SelectionChanged 事件

转载 作者:太空宇宙 更新时间:2023-11-03 15:44:53 25 4
gpt4 key购买 nike

我有一个包含 DataGridComboBoxColumn 的 DataGrid。我想要做的是根据此组合框的值(禁用)启用其他列。为此,我通过 ICommand 处理 SelectionChanged 事件,但从未调用此命令。它适用于 DataGrid 外部的组合框,但 DataGrid 内部的技巧是什么?

首先要注意的是,仅当该行不再处于编辑模式时才设置 Options 的值。

第二点,当按回车键结束编辑时,即使已使用组合框更改选项也未设置

 <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>

在虚拟机中

  private RelayCommand enableCustomParameters;
private Model.AcquisitionParameters selectedParameters;
private ObservableCollection<Model.AcquisitionParameters> acquisitionList = new ObservableCollection<Model.AcquisitionParameters>();

public ObservableCollection<Model.AcquisitionParameters> AcquisitionList
{
get { return acquisitionList; }
set { acquisitionList = value; OnPropertyChanged("AcquisitionList"); }
}

public Model.AcquisitionParameters SelectedParameters
{
get { return selectedParameters; }
set { selectedParameters = value; OnPropertyChanged("SelectedParameters"); }
}

public ICommand EnableCustomParameters
{
get
{
if(this.enableCustomParameters == null)
{
this.enableCustomParameters = new RelayCommand(
param => this.ChangeCustomState());
}
return this.enableCustomParameters;
}
}

public void ChangeCustomGainState()
{
SelectedParameters.CustomGainParametersEnabled = SelectedParameters.GainModeOptions == GainModeOptions.Custom;
}

和模型

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"); }
}
}

最佳答案

您应该使用适当的绑定(bind):

{Binding Path=yourCommandName, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}

here 阅读更多关于不同绑定(bind)的信息.

关于c# - MVVM 中的 DataGridComboBoxColumn SelectionChanged 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28549275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com