Search code examples
c#wpfmvvmfodyfody-propertychanged

PropertyChanged isn't working as expected


I'm currently trying myself on creating a WPF project with MVVM using Fody Property changed.

<Window x:Class="TestMVVM.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:TestMVVM"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525"
    DataContext="{x:Static local:MainWindowViewModel.Instance}"
    x:Name="WindowElement">

<StackPanel Orientation="Horizontal">        
    <TextBlock Text="{Binding Text, Mode=TwoWay}" />
    <Button Content="Browse" Command="{Binding WSDLBrowseClick}"/> 
</StackPanel>

public static class Model
{
    public static string text { get; set; }
}

public class MainWindowViewModel : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { }; 

    public static MainWindowViewModel Instance => new MainWindowViewModel();

    public string Text { get; set; }
    /*
    {
        get { return Model.text; }
        set
        {
            if (value == Text)
                return;

            Model.text = value;

            PropertyChanged(this, new PropertyChangedEventArgs("Text"));
        }
    }*/

    public ICommand WSDLBrowseClick { get; set; }


    public MainWindowViewModel()
    {
        WSDLBrowseClick = new RelayCommand(BrowseWSDL);
    }


    private void BrowseWSDL()
    {
        Text = "Test";           
    }
}

Basically I want the TextBlock to show the "Test"-Text when I click the button. The Click-Command gets executed but the TextBlock's text doesn't change. I want to use the property Text as a local memory that keeps the textblock up to date so I can later send the value to model.text and use it there. But it only works if I use the code that I have currently commented out. Isn't fody weaver supposed to do the same thing for me (just that he creates another private variable instead of using model.text)?


Solution

  • I managed to make it work by adding <PropertyChanged/> to my FodyWeavers.xml