C# 使用MVVM在wpf中处理标签的内容更改事件

C# 使用MVVM在wpf中处理标签的内容更改事件,c#,wpf,C#,Wpf,此标签存在于用户控件中,我正在将此标签的内容属性绑定到窗口的datacontext。每当此标签的内容发生更改时,我都要执行ICommand。它绑定到的属性位于父视图模型中。而我在用户控件的viewmodel中有一个ICommand <Label Style="{StaticResource LabelStyle}" FontSize="28" HorizontalAlignment="Center" Content="{Binding Rel

此标签存在于用户控件中,我正在将此标签的内容属性绑定到窗口的datacontext。每当此标签的内容发生更改时,我都要执行ICommand。它绑定到的属性位于父视图模型中。而我在用户控件的viewmodel中有一个ICommand

    <Label Style="{StaticResource LabelStyle}" 
         FontSize="28" HorizontalAlignment="Center" 
         Content="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}, 
         Mode=FindAncestor}, Path=DataContext.CurrentTag}">
    </Label>
标签没有要处理的ContentChanged事件

您可以使用DependencyPropertyDescriptor并以编程方式使用其AddValueChanged方法连接事件处理程序:

public partial class MyUserControl : UserControl
{
    public MyUserControl ()
    {
        InitializeComponent();
    DataContext = new YourViewModelClass();

        DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(Label.ContentProperty, typeof(Label));
        if (dpd != null)
        {
            dpd.AddValueChanged(label, OnLabelContentChanged);
        }
    }

    private void OnLabelContentChanged(object sender, EventArgs e)
    {
        var vm = this.DataContext as YourViewModelClass;
        vm.YourCommand.Execute(null);
    }
}
处理对视图中依赖项属性的更改:


我发现了一种更干净的方法,适合MVVM模式

交互性来自名称空间的下方

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
设置NotifyOnTargetUpdated=True,使TargetUpdated事件能够在我绑定ICommand的位置触发

 <Label Style="{StaticResource LabelStyle}" FontSize="28" HorizontalAlignment="Center" 
             Content="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}
                        ,Mode=FindAncestor}
            , Path=DataContext.CurrentTag, NotifyOnTargetUpdated=True}" >
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="TargetUpdated">
                    <i:InvokeCommandAction Command="{Binding SubmitCommand}" 
                                           CommandParameter="{Binding 
                               RelativeSource={RelativeSource AncestorType={x:Type Window}
                                ,Mode=FindAncestor}, Path=DataContext.CurrentTag}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Label>
如前所述,Label没有ContentChanged事件

然而,当我需要这个属性工作时,我使用了SizeChanged事件

我所做的是,每当我更改标签的内容时,我都会使用接近原始宽度的值更改标签的宽度

由于标签不显示边,所以标签的长度无关紧要,除非附近没有任何其他工具


我知道这不是解决这个问题的精确和正式的解决方案,但它挽救了我的一天。

我在这里遇到了一个问题,我在ICommand中获得了旧的价值,但我正在寻找新的价值
 <Label Style="{StaticResource LabelStyle}" FontSize="28" HorizontalAlignment="Center" 
             Content="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}
                        ,Mode=FindAncestor}
            , Path=DataContext.CurrentTag, NotifyOnTargetUpdated=True}" >
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="TargetUpdated">
                    <i:InvokeCommandAction Command="{Binding SubmitCommand}" 
                                           CommandParameter="{Binding 
                               RelativeSource={RelativeSource AncestorType={x:Type Window}
                                ,Mode=FindAncestor}, Path=DataContext.CurrentTag}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Label>