c# - 嵌套 UserControl 事件在 MVVM/WPF 场景中不适用于 EventTrigger/InvokeCommandAction

标签 c# wpf events mvvm prism

我正在使用带有 Prism (MVVM) 的 WPF,并尝试为一些类构建一个检查器。这些类之一是 Vector3:

<Grid x:Name="Vector3Root" Background="White">
    <StackPanel Orientation="Horizontal">
        <StackPanel Orientation="Horizontal">
            <xctk:DoubleUpDown Tag="X" Style="{StaticResource DoubleUpDownStyle}" Value="{Binding X}" ValueChanged="Vector3ValueChanged"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <xctk:DoubleUpDown Tag="Y" Style="{StaticResource DoubleUpDownStyle}" Value="{Binding Y}" ValueChanged="Vector3ValueChanged"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <xctk:DoubleUpDown Tag="Z" Style="{StaticResource DoubleUpDownStyle}" Value="{Binding Z}" ValueChanged="Vector3ValueChanged"/>
        </StackPanel>
    </StackPanel>
</Grid>

及其隐藏代码

namespace SimROV.WPF.Views{
public partial class Vector3View : UserControl
{
    public Vector3View()
    {
        InitializeComponent();
    }

    public static readonly RoutedEvent SettingConfirmedEvent =
        EventManager.RegisterRoutedEvent("SettingConfirmed", RoutingStrategy.Bubble,
        typeof(RoutedEventHandler), typeof(Vector3View));

    public event RoutedEventHandler SettingConfirmed
    {
        add { AddHandler(SettingConfirmedEvent, value); }
        remove { RemoveHandler(SettingConfirmedEvent, value); }
    }

    public void Vector3ValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        RaiseEvent(new RoutedEventArgs(SettingConfirmedEvent));
    }
}}

我正在努力解决的问题是,我无法捕获另一个 ValueChanged 上触发的事件( SettingConfirmedUserControl )。正在使用 Vector3View 的 ViewModel :

<UserControl
         x:Class="SimROV.WPF.Views.TransformView"
         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:views="clr-namespace:SimROV.WPF.Views"
         xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
         xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
         xmlns:prism="http://prismlibrary.com/"

mc:Ignorable="d" >

<Grid x:Name="TransformRoot" Background="White">
    <StackPanel Orientation="Vertical">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Position" Margin="5"/>
            <!--<ContentPresenter ContentTemplate="{StaticResource Vector3Template}"/>-->
            <views:Vector3View x:Name="PositionVector3">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SettingConfirmed">
                        <prism:InvokeCommandAction Command="{Binding PositionValueChangedCommand}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </views:Vector3View>
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Rotation" Margin="5"/>
            <!--<ContentPresenter ContentTemplate="{StaticResource Vector3Template}"/>-->
            <views:Vector3View x:Name="RotationVector3" SettingConfirmed="RotationValueChangedEvent"/>
        </StackPanel>
    </StackPanel>
</Grid>

此时我可以捕获 SettingConfirmedRotationValueChangedEvent在代码隐藏上,但由于我遵循 MVVM 模式,这对我不起作用,这就是我使用 EventTrigger 的原因和InvokeCommandAction捕捉这些事件TransformViewModel ,但那些人永远不会被解雇。 这是TransformViewModel :

namespace SimROV.WPF.ViewModels{
public class TransformViewModel : BindableBase
{
    private ICommand _positionCommand;

    public ICommand PositionValueChangedCommand => this._positionCommand ?? (this._positionCommand = new DelegateCommand(PositionChanged));
    private void PositionChanged()
    {

    }
    public TransformViewModel()
    {

    }
}}

PositionChanged只是从来没有被解雇过,我根本不明白为什么。

我不知道这是否相关,但 Transform 是 ObservableCollection<IComponent> 的一个元素在另一个 ViewModel 中,它由 ListView 呈现与 ItemContainerStyle ,有 ContentPresenter里面有一个 ContentTemplateSelector。

有人可以告诉我为什么会发生这种情况以及如何解决它吗?

谢谢。

最佳答案

您的EventTriggerInvokeCommandAction应该可以正常工作,前提是 DataContext Vector3View的实际上是一个TransformViewModel所以绑定(bind)到 PositionValueChangedCommand属性(property)成功。

关于c# - 嵌套 UserControl 事件在 MVVM/WPF 场景中不适用于 EventTrigger/InvokeCommandAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53432795/

相关文章:

c# - 企业库 Unity 与其他 IoC 容器

c# - HttpClient 抛出 System.ArgumentException : 'windows-1251' is not a supported encoding name

c# - 构建支持我自己的自定义事件的 EventTriggerBehavior

php - Magento - customer_save_after 总是解雇两次

c# - 使用 DLLImport 时异常如何工作

c# - 如何使用 NPOI 在 C# 中使用列名获取 excel 单元格值

c# - ObserveOnDispatcher 不工作

wpf - 如何在 WPF 中创建 Windows 8 风格的应用栏?

javascript - 似乎无法让 jQuery .on 启动

c# - 如何通过 C# 反射检查一个方法是否可以作为事件的处理程序?