C# WPF ListView更改组合框单元格中的选定项项目焦点或更改

C# WPF ListView更改组合框单元格中的选定项项目焦点或更改,c#,wpf,listview,combobox,C#,Wpf,Listview,Combobox,对于我的ListView,行中有几个组合框。我还有一个绑定到所选行的文本框,用于显示ListView下面行中的其他信息。问题是,当您单击行中的组合框时,该行的ListView选定项/索引不会更改。当选中某行中的组合框时,如何更改该行的ListView中的选定项 以下是我的列表视图和组合框: <ListView Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4" VerticalAlignment="Stretch" Items

对于我的ListView,行中有几个组合框。我还有一个绑定到所选行的文本框,用于显示ListView下面行中的其他信息。问题是,当您单击行中的组合框时,该行的ListView选定项/索引不会更改。当选中某行中的组合框时,如何更改该行的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>


将事件处理程序添加到您的
样式中
,并处理
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);
}
private void lv_PreviewMouseLeftButtonDown(对象发送器,鼠标按钮ventargs e)
{
ListViewItem lvi=(ListViewItem)发送方;
ListView lv=FindParent(lvi);
lv.SelectedItem=lvi.DataContext;
}
私有静态T FindParent(DependencyObject DependencyObject),其中T:DependencyObject
{
var parent=VisualTreeHelper.GetParent(dependencyObject);
if(parent==null)返回null;
var parentT=作为T的父项;
返回父母??找到父母(父母);
}
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模式。

非常感谢,它的工作非常有魅力。为此挣扎了两天。再次感谢!