c# - ListBox 中的 ComboBox 不会触发 SelectionChanged 事件

标签 c# wpf xaml mvvm

我有一个 wpf,mvvm 应用程序,使用 catel (http://catel.codeplex.com) 框架\工具包,C# .Net 4.0。该应用程序有一个带有 TextBlock 和 ComboBox 的 ListBox。 ListBox 和 ComboBox 由 ViewModel 中的 2 个不同的 ObservableCollection 填充。我需要保存(到数据库),当用户单击按钮时,列表框中的每一行用户从组合框中选择了一个项目。对于 ListBox 中的任何 ComboBoxes,SelectionChanged 事件都不会触发。这个想法是我添加到一个列表(ArrayList 或 IList?),在 ViewModel 中,每次用户在 ComboBox 中选择一个项目以及该项目已被选择的行。

还是我尝试使用 ComboBoxe SelectionChanged 事件以错误的方式解决这个问题?我也尝试通过 ListBox.Items 进行迭代,但这似乎是一个问题,如果可能的话,我想避免 ViewModel 中的 ui 元素逻辑。

xml:

<Grid>
<StackPanel Orientation="Horizontal">
    <Label Width="180">Field1</Label>
    <ListBox Height="200" 
            IsSynchronizedWithCurrentItem="True" 
            ItemsSource="{Binding List1, Mode=OneWay}" 
            Name="listBox1" 
            SelectionMode="Single" 
            Width="300">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" Width="290">
                    <TextBlock Width="90" Text="{Binding}"></TextBlock>
                        <ComboBox Width="180" ItemsSource="{Binding DataContext.List2, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" DisplayMemberPath="Field1">
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="SelectionChanged">
                                    <catel:EventToCommand Command="{Binding SelectionChangedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}" DisableAssociatedObjectOnCannotExecute="False" PassEventArgsToCommand="True" />
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        </ComboBox>
                    </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</StackPanel>

View 模型代码:
//in the ViewModel constructor

SelectionChangedCommand = new Command<SelectionChangedEventArgs>(OnSelectionChangedCommandExecute, OnSelectionChangedCommandCanExecute);



public Command<SelectionChangedEventArgs> SelectionChangedCommand { get; private set; }

private bool OnSelectionChangedCommandCanExecute()
{
    return true;
}

private void OnSelectionChangedCommandExecute(SelectionChangedEventArgs e)
{
    // add or update list....
}

最佳答案

在命令绑定(bind)中,您使用了具有相对源绑定(bind)的绑定(bind)...

考虑在绑定(bind)中进行这些更改

1) 使用列表框作为祖先类型

2) 绑定(bind)时使用 Path=DataContext.SelectionChangedCommand 否则它将列表框作为数据上下文。

<catel:EventToCommand Command="{Binding Path=DataContext.SelectionChangedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}" DisableAssociatedObjectOnCannotExecute="False" PassEventArgsToCommand="True" />

关于c# - ListBox 中的 ComboBox 不会触发 SelectionChanged 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8746566/

相关文章:

c# - 解析 HTML 文档?

wpf - 我可以通过样式添加硬编码的 ComboBoxItems 吗?

c# - 对象当前正在别处使用 - 当 image.save

c# - 从另一个 View 的中心开始到其左边界的水平展开动画

c# - WPF 中的列表框分隔符和最终分隔符的省略

c# - Windows Phone 7 Mango 的 FTP 客户端?

c# - 设置 ItemSource ComboBox 属性时出现 NullReferenceException

windows-phone-7 - 我在哪里可以找到静态资源?

c# - 如何在C#中实例化堆中的结构

c# - 嵌套控件结构 - 使用 XAML 还是 C#?