【问题标题】:Property doesn't update when set in the Business Logic在业务逻辑中设置时属性不会更新
【发布时间】:2016-05-07 12:05:03
【问题描述】:

我需要使用业务逻辑中的方法在业务逻辑中设置属性。如果您运行我的代码,您可以看到第一个字符串“目标位置”成功更改,但第二个“其他字符串”不会更改其在视图中的值。 BusinessLogic.cs 中的“PropertyChanged”为空。我完全不知道为什么它是空的!有人可以解释一下这种行为以及如何解决这个问题吗?

我的项目中有以下文件:

MainWindow.xaml

<Window x:Class="TestWpf.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:TestWpf"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<StackPanel>
    <TextBox Text="{Binding Path=TargetLocation}"></TextBox>
    <TextBox Text="{Binding Path=SomeOtherString}"></TextBox>
    <Button Click="ChangeTextButton_Click">Change Target Location</Button>
    <Button Click="ChangeSomeOtherStringButton_Click">Change some other string</Button>
</StackPanel>

MainWindow.xaml.cs

public MainWindow()
{
    InitializeComponent();

    MainViewModel mainViewModel = new MainViewModel();
    mainViewModel.TargetLocation = @"A:\Old_Location";
    mainViewModel.SomeOtherString = "Old String...";

    DataContext = mainViewModel;
}

private void ChangeTextButton_Click(object sender, RoutedEventArgs e)
{
    MainViewModel mainViewModel = (MainViewModel)DataContext;
    mainViewModel.TargetLocation = @"B:\New_Location";
}

private void ChangeSomeOtherStringButton_Click(object sender, RoutedEventArgs e)
{
    MainViewModel mainViewModel = (MainViewModel)DataContext;
    mainViewModel.ChangeSomeOtherString();
}

MainViewModel.cs

public class MainViewModel : INotifyPropertyChanged
{
    private string targetLocation;

    public string TargetLocation
    {
        get
        {
            return targetLocation;
        }
        set
        {
            targetLocation = value;
            OnPropertyChanged("TargetLocation");
        }
    }

    public string SomeOtherString
    {
        get
        {
            return BusinessLogicClass.GetInstance().SomeOtherString;
        }
        set
        {
            BusinessLogicClass.GetInstance().SomeOtherString = value;
            OnPropertyChanged("SomeOtherString");
        }
    }

    public void ChangeSomeOtherString()
    {
        BusinessLogicClass.GetInstance().ChangeSomeOtherString();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

业务逻辑类

public class BusinessLogicClass : INotifyPropertyChanged
{
    private static BusinessLogicClass instance;

    public static BusinessLogicClass GetInstance()
    {
        if (instance == null)
        {
            instance = new BusinessLogicClass();
        }

        return instance;
    }

    private BusinessLogicClass()
    {

    }

    private string someOtherString;

    public string SomeOtherString
    {
        get
        {
            return someOtherString;
        }
        set
        {
            someOtherString = value;
            OnPropertyChanged("SomeOtherString");
        }
    }

    public void ChangeSomeOtherString()
    {
        SomeOtherString = "New String!";
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

【问题讨论】:

    标签: c# wpf xaml inotifypropertychanged


    【解决方案1】:

    BusinessLogic.cs 中的“PropertyChanged”为空。我完全不知道为什么它是空的!

    BusinessLogic 类中的PropertyChanged 为空,因为没有使用此类中的属性作为其源的绑定。您的两个绑定的源属性都在您的 MainViewModel 类上。

    WPF 不会扫描所有碰巧实现INotifyPropertyChanged 的类。即使确实如此,它怎么知道从您的BusinessLogic 类触发的PropertyChanged 事件意味着它需要更新绑定到SomeOtherString 上的SomeOtherString 属性的TextBox? WPF 无法读取您的代码来找出这一点。

    最简单的解决方法是在 ChangeSomeOtherString() 方法中触发 PropertyChanged 事件:

        public void ChangeSomeOtherString()
        {
            BusinessLogicClass.GetInstance().ChangeSomeOtherString();
            OnPropertyChanged("SomeOtherString");    // Add this line
        }
    

    这样 WPF 就知道 SomeOtherString 属性的值已更改,并将对 TextBox 执行必要的更新。

    【讨论】:

    • 非常感谢您的解释!该修复适用于我:)
    猜你喜欢
    • 2012-11-08
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 2019-04-22
    • 1970-01-01
    相关资源
    最近更新 更多