C# wpf中的鼠标绑定手势未绑定到DataContext wpf

C# wpf中的鼠标绑定手势未绑定到DataContext wpf,c#,wpf,xaml,C#,Wpf,Xaml,我不明白为什么当用户双击listview项时,我的命令没有正确执行。我知道一个事实,错误发生在它的绑定方式上。我不知道如何正确绑定到windows数据上下文 如何从嵌套控件绑定到windows数据上下文 这里是有问题的代码隔离 <Grid.InputBindings> <MouseBinding Gesture="LeftDoubleClick" Command="{Binding EditCustomerCommand}"/> </Grid.InputBi

我不明白为什么当用户双击listview项时,我的命令没有正确执行。我知道一个事实,错误发生在它的绑定方式上。我不知道如何正确绑定到windows数据上下文

如何从嵌套控件绑定到windows数据上下文

这里是有问题的代码隔离

<Grid.InputBindings>
    <MouseBinding Gesture="LeftDoubleClick" Command="{Binding EditCustomerCommand}"/>
</Grid.InputBindings>

main window.xaml

<Window.DataContext>
        <viewModel:MainWindowViewModel />
    </Window.DataContext>

    <!--<Grid>
        <ContentPresenter Content="{Binding ActiveViewModel}"></ContentPresenter>
    </Grid>-->


    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <Button Grid.Row="0" Content="Add New Person" Command="{Binding AddNewPersonCommand}"/>
        <ListView Grid.Row="1" ItemsSource="{Binding People}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="First Name" Width="150">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Grid Margin="0">
                                    <StackPanel Orientation="Horizontal">
                                        <Button Content="X" Width="20" 
                                                Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.RemovePersonCommand}"
                                                CommandParameter="{Binding DataContext, RelativeSource={RelativeSource Self}}"
                                                />
                                        <Label  Content="{Binding FirstName}"/>
                                    </StackPanel>
                                    <Grid.InputBindings>
                                        <MouseBinding Gesture="LeftDoubleClick" Command="{Binding EditCustomerCommand}"/>
                                    </Grid.InputBindings>
                                </Grid>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>

            <ListView.Resources>
                <Style TargetType="{x:Type ListViewItem}">
                    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
                </Style>
            </ListView.Resources>

            <ListView.InputBindings>
                <KeyBinding Key="Delete" Command="{Binding DeleteCustomersCommand}"/>
            </ListView.InputBindings>

        </ListView>

        <StackPanel Grid.Row="2" Orientation="Horizontal">
            <Label Content="# People"/>
            <Label Content="{Binding PersonCount}"/>
        </StackPanel>

    </Grid>
</Window>

请尝试下一个解决方案,这里我使用了freezable代理类来访问主视图模型。这种情况与您的问题类似,因为列无法直接访问其父数据上下文

Xaml代码

<Window x:Class="DataGridSoHelpAttempt.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dataGridSoHelpAttempt="clr-namespace:DataGridSoHelpAttempt"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    Title="MainWindow" Height="350" Width="525" x:Name="This">
<Window.DataContext>
    <dataGridSoHelpAttempt:MainViewModel/>
</Window.DataContext>
<Grid x:Name="MyGrid">
    <Grid.Resources>
        <dataGridSoHelpAttempt:FreezableProxyClass x:Key="ProxyElement" ProxiedDataContext="{Binding Source={x:Reference This}, Path=DataContext}"/>
    </Grid.Resources>
    <DataGrid x:Name="MyDataGrid" ItemsSource="{Binding DataSource}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
            <DataGridTextColumn Header="Description" Binding="{Binding Description}"  Visibility="{Binding Source={StaticResource ProxyElement}, 
                Path=ProxiedDataContext.Visibility, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
            <DataGridTextColumn Header="Comments" Binding="{Binding Comments}" Width="150"/>
            <DataGridTextColumn Header="Price (click to see total)" Binding="{Binding Price, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
        </DataGrid.Columns>
    </DataGrid>
    <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Bottom">
        <Button Content="Show Description" Command="{Binding Command}"></Button>
    </StackPanel>
</Grid>
 public class FreezableProxyClass : Freezable
{
    protected override Freezable CreateInstanceCore()
    {
        return new FreezableProxyClass();
    }


    public static readonly DependencyProperty ProxiedDataContextProperty = DependencyProperty.Register(
        "ProxiedDataContext", typeof (object), typeof (FreezableProxyClass), new PropertyMetadata(default(object)));

    public object ProxiedDataContext
    {
        get { return (object) GetValue(ProxiedDataContextProperty); }
        set { SetValue(ProxiedDataContextProperty, value); }
    }
}
所有这些都是由于目标代码而完成的,请将解决方案作为您研究的起点。如果您在代码方面遇到问题,我很乐意提供帮助

问候。
我很乐意帮助你

listview正在吞下鼠标event@thumbmunkeys你有什么建议吗?用ItemsControl代替ListView应该行得通:什么?!不,那根本不行。我必须进去设置所有的选择命令等等。那是不对的。
 public class FreezableProxyClass : Freezable
{
    protected override Freezable CreateInstanceCore()
    {
        return new FreezableProxyClass();
    }


    public static readonly DependencyProperty ProxiedDataContextProperty = DependencyProperty.Register(
        "ProxiedDataContext", typeof (object), typeof (FreezableProxyClass), new PropertyMetadata(default(object)));

    public object ProxiedDataContext
    {
        get { return (object) GetValue(ProxiedDataContextProperty); }
        set { SetValue(ProxiedDataContextProperty, value); }
    }
}