【问题标题】:MouseBinding Gesture in wpf not binding to DataContext wpfwpf中的MouseBinding手势未绑定到DataContext wpf
【发布时间】:2016-04-20 10:03:16
【问题描述】:

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

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

这里是有问题的代码被隔离...

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

MainWindow.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>

【问题讨论】:

  • 列表视图正在吞噬鼠标事件
  • @thumbmunkeys 你有什么建议吗?
  • 使用 ItemsControl 而不是 ListView 应该可以工作:stackoverflow.com/questions/3356719/…
  • 什么?!不,那根本行不通。我将不得不进入并设置所有选择命令,而不是什么。这是不正确的。

标签: c# wpf xaml


【解决方案1】:

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

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 MainViewModel:BaseObservableObject
{
    private Visibility _visibility;
    private ICommand _command;
    private Visibility _totalsVisibility;
    private double _totalValue;

    public MainViewModel()
    {
        Visibility = Visibility.Collapsed;
        TotalsVisibility = Visibility.Collapsed;
        DataSource = new ObservableCollection<BaseData>(new List<BaseData>
        {
            new BaseData {Name = "Uncle Vania", Description = "A.Chekhov, play", Comments = "worth reading", Price = 25},
            new BaseData {Name = "Anna Karenine", Description = "L.Tolstoy, roman", Comments = "worth reading", Price = 35},
            new BaseData {Name = "The Master and Margarita", Description = "M.Bulgakov, novel", Comments = "worth reading", Price = 56},
        });
    }

    public ICommand Command
    {
        get
        {
            return _command ?? (_command = new RelayCommand(VisibilityChangingCommand));
        }
    }

    private void VisibilityChangingCommand()
    {
        Visibility = Visibility == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed;
    }

    public ObservableCollection<BaseData> DataSource { get; set; }

    public Visibility Visibility
    {
        get { return _visibility; }
        set
        {
            _visibility = value;
            OnPropertyChanged();
        }
    }

    public ObservableCollection<BaseData> ColumnCollection
    {
        get { return DataSource; }
    }

    public Visibility TotalsVisibility
    {
        get { return _totalsVisibility; }
        set
        {
            _totalsVisibility = value;
            OnPropertyChanged();
        }
    }

    public double TotalValue
    {
        get { return ColumnCollection.Sum(x => x.Price); }
    }

}

public class BaseData:BaseObservableObject
{
    private string _name;
    private string _description;
    private string _comments;
    private int _price;

    public virtual string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            OnPropertyChanged();
        }
    }

    public virtual object Description
    {
        get { return _description; }
        set
        {
            _description = (string) value;
            OnPropertyChanged();
        }
    }

    public string Comments
    {
        get { return _comments; }
        set
        {
            _comments = value;
            OnPropertyChanged();
        }
    }

    public int Price
    {
        get { return _price; }
        set
        {
            _price = value;
            OnPropertyChanged();
        }
    }
}

可冻结的代理代码

 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); }
    }
}

由于freezable目标代码,一切都完成了,请以解决方案为起点进行研究。如果您遇到代码问题,我很乐意提供帮助。

问候。 我很乐意帮助你

【讨论】:

    猜你喜欢
    • 2012-11-05
    • 2011-07-07
    • 2013-11-16
    • 2013-02-02
    • 1970-01-01
    • 2021-07-19
    • 2010-11-01
    • 1970-01-01
    • 2012-12-25
    相关资源
    最近更新 更多