WPF标签绑定未正确更新

xyhw6mcr  于 2023-02-13  发布在  其他
关注(0)|答案(2)|浏览(186)

我知道关于这个主题有上千个问题,但我找不到我的问题出在哪里......
我有一个. xaml.cs文件,它有标准的notify属性changed stuff,一个公共成员,在setter中有"notify",我还在构造函数中设置了数据上下文,如下所示:

public partial class SlimERDplot : UserControl
{
   public SlimERDplot()
   {
      DataContext = this;
      InitializeComponent();
   }

   public enum AvailableImaginations { NotSet, Right, Left}
   private AvailableImaginations _movementImagination = AvailableImaginations.NotSet;

   //This is the one I'll be binding
   public string HemisphereUI { get { return Hemisphere.ToString() + " hemisphere"; } }

   public AvailableHemispheres Hemisphere
   {
      get { return _hemisphere; }
      set
      {
         if (_hemisphere != value)
         {
            _hemisphere = value;
            NotifyPropertyChanged();
            NotifyPropertyChanged("HemisphereUI"); //tried with or without this, 
                                                    //makes no difference
         }
      }
   }
   

   public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
       var handler = PropertyChanged;
       if (handler != null)
          handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

XAML简单地:

<Label HorizontalAlignment="Right" Content="{Binding Path=HemisphereUI}"></Label

然后,从使用者XAML中,我这样使用它:

<UIControls:SlimERDplot x:Name="ERDRightDown" Hemisphere="Right" MovementImagination="Left" />

但是在标签中,它总是说NotSet(默认值),我调试了一下,这个东西确实用正确的值通过了setter,但是显示的值从来没有改变
少了什么?

smdncfj3

smdncfj31#

必须使用DependencyProperty
这里有一个例子

代码隐藏

public partial class MyUserControl : UserControl
{
    public MyUserControl()
    {
        InitializeComponent();
    }

    public string HemisphereUI
    {
        get { return (string)GetValue( HemisphereUIProperty ); }
        private set { SetValue( HemisphereUIProperty, value ); }
    }

    // Using a DependencyProperty as the backing store for HemisphereUI.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty HemisphereUIProperty =
        DependencyProperty.Register( nameof( HemisphereUI ), typeof( string ), typeof( MyUserControl ), new PropertyMetadata( null ) );

    public AvailableHemispheres Hemisphere
    {
        get { return (AvailableHemispheres)GetValue( HemisphereProperty ); }
        set { SetValue( HemisphereProperty, value ); }
    }

    // Using a DependencyProperty as the backing store for Hemisphere.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty HemisphereProperty =
        DependencyProperty.Register( nameof( Hemisphere ), typeof( AvailableHemispheres ), typeof( MyUserControl ), new PropertyMetadata( AvailableHemispheres.NotSet, OnHemisphereChanged ) );

    private static void OnHemisphereChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
    {
        MyUserControl control = ( d as MyUserControl )!;
        control.HemisphereUI = e.NewValue.ToString() + " hemisphere";
    }
}

public enum AvailableHemispheres
{
    NotSet,
    Right,
    Left,
}

XAML语言

<UserControl
  x:Class="WpfApp1.Views.MyUserControl"
  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:local="clr-namespace:WpfApp1.Views"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  d:DesignHeight="450"
  d:DesignWidth="800"
  mc:Ignorable="d">
  <Grid>
    <Label
      HorizontalAlignment="Center"
      VerticalAlignment="Center"
      Content="{Binding HemisphereUI, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MyUserControl}}}" />
  </Grid>
</UserControl>
ee7vknir

ee7vknir2#

少了什么?
您的UserControl没有实现INotifyPropertyChanged

public partial class SlimERDplot : UserControl, INotifyPropertyChanged
{
    ...
}

这是视图实际订阅PropertyChanged事件所必需的。

相关问题