gpt4 book ai didi

c# - 如何从附加的行为触发组合框 SelectionChanged 事件

转载 作者:行者123 更新时间:2023-12-03 10:20:49 28 4
gpt4 key购买 nike

我有一些 ComboBox控件,每个控件都有一些值。每个选定的值都会触发我的一个事件。
当从 ComboBoxA 中选择项目时,我的事件将使用所选项目的值触发。
当我的一个组合框刚刚打开和关闭时,其中没有更改任何值,则不会触发事件(这是组合框的默认行为)。
但这不是我需要的。

我所做的是创建一个与 DropDownClosed 相关联的行为。事件,但我不知道如何触发 SelectionChanged事件。

编辑:

这个问题可以概括为:
如何“手动”触发 UI 控件的事件?
或者 - 有没有办法调用与事件相关的方法?

编辑 2:

我会尝试更清楚地解释这个问题。我有这段代码,它接受一个项目列表,并将它们显示为类别(单选按钮)和类别下的项目(单选按钮内的组合框)。
选择项目时 - 触发选择更改事件。好的!
选择另一个单选按钮时 - 触发事件。好的!!

不起作用的特殊情况(作为 ComboBox 行为的默认设置):当打开未选择的组合之一并且在不更改其选择的情况下关闭它时 - 不会触发任何事件。但是 - 这个组合在我现在想要选择的未选择类别下,因为用户已经选择了它。我的想法是使用行为(在 xaml 代码中,但到目前为止还没有工作......)

代码(或多或少......):

<ListBox ItemsSource="{Binding Categories}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<RadioButton Content="{Binding CategotyName}" GroupName="TestCategory" IsChecked="{Binding IsSelected}"
cal:Message.Attach="SelectionChanged($dataContext)]"/>
<ComboBox cal:Message.Attach="SelectionChanged($dataContext)]"
ItemsSource="{Binding TestsNamesUnderCategory}" SelectedIndex="{Binding SelectedTestInx, Mode=TwoWay}">
<i:Interaction.Behaviors>
<local:ComboBoxReSelectionBehavior />
</i:Interaction.Behaviors>
</ComboBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

cal:Message.Attach="SelectionChanged($dataContext)]"正在使用 Caliburn 框架,它只是将触发器发送到我的方法。

希望这现在更清楚了。谢谢!

最佳答案

为什么您不只是将选择绑定(bind)到您的 View 模型属性,然后执行那里需要的任何逻辑?

xml:

<...
<ComboBox ItemsSource={Binding YourSource} SelectedItem={Binding YourSelectedItem}/>
.../>

View 模型:
private string yourItem; // assuming we are just dealing with strings...
public String YourItem {
get { return yourItem; }
set {
if( String.Equals(yourItem, value, StringComparison.OrdinalIgnoreCase) )
return;

OnYourItemChanged(); // Do your logics here
RaisePropertyChanged("YourItem");
}
}

private void OnYourItemChanged()
{
// .. do stuff here
}

如果您绝对需要使用事件,请改用事件到命令...

假设您使用 System.Windows.Interactivity & Galasoft MVVM light

引用:
 xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"      
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"

xml:
<...
<ComboBox ItemsSource={Binding YourSource} SelectedItem={Binding YourSelectedItem}>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<cmd:EventToCommand Command="{Binding SomeCommand}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
.../>

这会将您的事件连接到一个命令,该命令将在 View 模型上执行,并按要求提供参数。

View 模型:
 public class YourViewModel : ViewModelBase 
{
public ICommand SomeCommand { get; set; }

public YourViewModel(......)
{
SomeCommand = new RelayCommand<SelectionChangedEventArgs>(YourCommandMethod);
}

private void YourCommandMethod(SelectionChangedEventArgs e)
{
// Do your magic here....
}
}

注意我在这台计算机上没有任何访问 vs 的情况下写了这个......有很多关于如何做到这一点的例子。希望能帮助到你。

关于c# - 如何从附加的行为触发组合框 SelectionChanged 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24905875/

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