【问题标题】:Label Content does not change with binding [duplicate]标签内容不会随绑定而改变[重复]
【发布时间】:2018-11-01 01:55:24
【问题描述】:

我目前在更新标签内容时遇到问题。我在这里缺少什么以更新Current_Plan 属性更新吗?实现见下文:

在我的 xaml 代码中,我有一个名为 LabelProductionPlan 的标签,设置如下:

   <Label x:Name="LabelProductionPlan" 
          Content="{Binding Current_Plan.ProductionPlanName, 
                            Mode=TwoWay,
                            UpdateSourceTrigger=PropertyChanged}"/>

在我的MainWindow.xaml.cs文件中,设置如下:

public partial class MainWindow : Window
    {

        private FrontEndManager FEM;

        public MainWindow()
        {
            InitializeComponent();
            WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;


            FEM = new FrontEndManager
            {
                Current_Plan = new ProductionPlanModel
                {

                }

            };

            LabelProductionPlan.DataContext = FEM;
        }}

FrontEndManager 设置了以下Fody feature 以实现 PropertyChanging:

[ImplementPropertyChanging]
public class FrontEndManager
{
    public ProductionPlanModel Current_Plan { get; set; }
}

ProductionPlanModel 也与PropertyChanging 一起实现:

[ImplementPropertyChanging]
public class ProductionPlanModel
{
    public string ProductionPlanName { get; set; }
}

【问题讨论】:

  • 我认为你使用了错误的 Fody 工具 - 你想要 Fody.PropertyChanged
  • 我刚刚找到了一个 INotifyPropertyChanging 的实际用例,而不是丑陋的“抛出异常以停止属性设置器”-stackoverflow.com/a/41472332/967885
  • @Peregrine 你是对的!你想提交那个以便我接受你的回答吗?非常感谢你抓住这个!

标签: c# wpf binding


【解决方案1】:

您正在使用的事件,正在更改,大多数依赖属性都不使用,例如您在Label 中绑定的Content。您需要使用INotifyPropertyChanged 事件,该事件在值完全更改并且可以使用时提供通知。如前所述,您似乎需要改用Fody.PropertyChanged

【讨论】:

    猜你喜欢
    • 2013-07-04
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-03
    • 2022-07-21
    相关资源
    最近更新 更多