【问题标题】:WPF ListView Change Selected Item on ComboBox cell item focus or changeWPF ListView 更改 ComboBox 单元格项目焦点上的选定项目或更改
【发布时间】:2018-07-02 15:47:36
【问题描述】:

对于我拥有的 ListView,行中有几个组合框。我还有一个绑定到所选行的文本框,以显示 ListView 下方行中的其他信息。问题是,当您连续单击 ComboBox 时,该行的 ListView 选定项/索引不会更改。选择该行中的 ComboBox 时,如何更改该行的 ListView 中的选定项?

这是我的带有 ComboBox 的 ListView:

<ListView Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4" VerticalAlignment="Stretch" 
        ItemsSource="{Binding Equations.DataExpressions}" SelectedItem="{Binding Equations.SelectedExpression}" SelectedIndex="0">
<ListView.Resources>
    <Style TargetType="{x:Type ListViewItem}">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="VerticalContentAlignment" Value="Stretch" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsValidExpression}" Value="false">
                <Setter Property="Background" Value="#FF8080" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</ListView.Resources>
<ListView.View>
    <GridView>
        <GridViewColumn Header="Path" Width="90">
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.PathItems}" 
                                SelectedValue="{Binding EvaluatedPath}" Margin="-6, 0, -6, 0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                    </ComboBox>
                </DataTemplate>
            </GridViewColumn.CellTemplate>
        </GridViewColumn>
    </GridView>
</ListView.View>

【问题讨论】:

    标签: c# wpf listview combobox


    【解决方案1】:

    将事件处理程序添加到您的Style 并处理PreviewMouseLeftButtonDown 事件:

    private void lv_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        ListViewItem lvi = (ListViewItem)sender;
        ListView lv = FindParent<ListView>(lvi);
        lv.SelectedItem = lvi.DataContext;
    }
    
    private static T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject
    {
        var parent = VisualTreeHelper.GetParent(dependencyObject);
    
        if (parent == null) return null;
    
        var parentT = parent as T;
        return parentT ?? FindParent<T>(parent);
    }
    

    XAML:

    <Style TargetType="{x:Type ListViewItem}">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="VerticalContentAlignment" Value="Stretch" />
        <EventSetter Event="PreviewMouseLeftButtonDown" Handler="lv_PreviewMouseLeftButtonDown" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsValidExpression}" Value="false">
                <Setter Property="Background" Value="#FF8080" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
    

    请注意,由于这纯粹是与视图/控制相关的逻辑,因此应在视图中实现(代码隐藏)。视图模型不负责控件的行为方式,因此这不会破坏 MVVM 模式。

    【讨论】:

    • 非常感谢,就像一个魅力。为此苦苦挣扎了两天。再次感谢!
    猜你喜欢
    • 2011-01-14
    • 2014-12-15
    • 1970-01-01
    • 2019-03-07
    • 2011-10-03
    • 2015-12-06
    • 1970-01-01
    • 2011-05-13
    • 2011-02-06
    相关资源
    最近更新 更多