看起来 Interaction.Triggers 不适用于 DataContext 集



我将从我的代码开始(整个示例在这里:https://github.com/robertwojnar/MvvmDemo1(在我的小演示中,我有带有用户控制的单视图应用程序。所以我有:

  1. MainWindow.xaml (我的观点(
  2. FooUserControl.xaml (查看我的用户控件(
  3. MainWindowViewModel.cs (viewmodel for my view(

基本上就是这样。很简单。这是代码:

FooUserControl.xaml

    <UserControl x:Class="MvvmDemo1.WPF.Views.FooUserControl"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:MvvmDemo1.WPF.Views"
                 mc:Ignorable="d" 
                 d:DesignHeight="100" d:DesignWidth="100">
        <Grid MouseDown="UIElement_OnMouseDown">
            <Rectangle Fill="BlueViolet" />
        </Grid>
    </UserControl>

FooUserControl (code-disi(

    public partial class FooUserControl : UserControl
    {
        public FooUserControl()
        {
            InitializeComponent();
        }   
        public event EventHandler<BarEventArgs> BarClick;
        private void UIElement_OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            double x = e.GetPosition(this).X;
            double y = e.GetPosition(this).Y;
            string value_to_pass = "[" + x + "," + y + "]"; 
            BarEventArgs bar = new BarEventArgs() { Bar = 2, Foo = value_to_pass };
            BarClick?.Invoke(sender, bar);
        }
    }

MainWindow.xaml(无代码隐藏(

    <Window x:Class="MvvmDemo1.WPF.Views.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:MvvmDemo1.WPF.Views"
            mc:Ignorable="d"
            Title="MainWindow" Height="300" Width="300"
            xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
            xmlns:cmd="http://www.galasoft.ch/mvvmlight"
            xmlns:viewModels="clr-namespace:MvvmDemo1.WPF.ViewModels">
        <Window.DataContext>
            <viewModels:MainWindowViewModel />
        </Window.DataContext>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Loaded">
                <cmd:EventToCommand Command="{Binding Mode=OneWay, Path=LoadedCommand}" PassEventArgsToCommand="True" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
        <Grid>
            <local:FooUserControl>
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="BarClick">
                        <cmd:EventToCommand Command="{Binding ClickedCommand}" PassEventArgsToCommand="True" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </local:FooUserControl>
        </Grid>
    </Window>

主窗口视图模型.cs

    public class MainWindowViewModel : ObservableObject
    {
        public string Title => "Main window";
        public ICommand LoadedCommand => new RelayCommand(Loaded);
        private void Loaded()
        {
            Debug.WriteLine("Loaded");
        }   
        public ICommand ClickedCommand => new RelayCommand<BarEventArgs>(o => Clicked(o.Foo));
        private void Clicked(string a)
        {
            Debug.WriteLine("Clicked " + a);
        }
    }

如您所见,该应用程序只是一个紫色矩形,它(单击事件(将单击坐标发送到MainWindowViewModel(通过ICommand,使用MVVM Light的EventToCommandInteraction.Triggers(。

这是我的问题和问题:

我想为我的用户控件添加一个视图模型。想要将FooUserControlViewModel添加到 FooUserControl 的DataContext中。问题是,当我设置FooUserControl的DataContext时,ClickedCommand没有被触发。问题是为什么?

当你克隆我的存储库并将FooUserControl(代码隐藏(构造函数更改为这个时,你会明白我的观点:

    public FooUserControl()
    {
        InitializeComponent();
        DataContext = new FooUserControlViewModel();
    }

编辑:看起来,MainWindowViewModel被分配给FooUserControl的DataContext(我认为,因为Iteraction.TriggersEventToCommand(。为什么会这样?这不是违反了视图-视图模型连接应为 1<->1 的规则吗?一个视图 - 一个视图模型?

如果要 local:FooUserControl分配新DatContext,可以执行此操作,但无法再直接从MainWindowViewModel绑定控件。您必须更改Binding,如下所示Command

<cmd:EventToCommand Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}},Path=DataContext.ClickedCommand}" PassEventArgsToCommand="True" />

当我们设置控件的DataContext时,它将自动 下到属性值继承的子控件。如果 您在中间更改UserControlDataContext 层次结构/可视化树 数据上下文将有所不同AncestorsDescendants.

也没有这样的规则 1 视图 -1 视图模型。这完全取决于您的设计复杂性/要求您必须为View设计多少ViewModels或为不同View设计一个ViewModel

相关内容

最新更新