在业务逻辑中设置时,属性不会更新

时间:2016-05-07 12:05:04

标签: c# wpf xaml inotifypropertychanged

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

我的项目中有以下文件:

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));
        }
    }
}

BusinessLogicClass

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));
        }
    }
}

1 个答案:

答案 0 :(得分:1)

  

&#34;的PropertyChanged&#34;在BusinessLogic.cs中为null。我完全不知道为什么它是空的!

BusinessLogic类中的

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

WPF无法扫描恰好实现INotifyPropertyChanged的所有类。即使它确实如此,如何知道从PropertyChanged类触发的BusinessLogic事件意味着它需要更新绑定到SomeOtherString属性MainViewModel的TextBox。 }? WPF无法读取您的代码以便找到它。

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

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

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