1

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)?

Theo
  • 31
  • 5
  • Looks correct, did you add the weaver to FodyWeavers.xml? Also try without the empty guard delegate on the PropertyChangedEvent, could be that Fody doesn't want to overwrite that. – Lennart Nov 10 '17 at 10:13
  • Your code works just fine using version 2.1.4 of Fody. – mm8 Nov 10 '17 at 14:43

3 Answers3

2

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

Theo
  • 31
  • 5
0

From what I can tell from the example you need to mark the class with [ImplementPropertyChanged] attribute.

Source

Igor Meszaros
  • 2,081
  • 2
  • 22
  • 46
  • This attribute is obsolete by now, implementing INotifyPropertyChanged on the class is enough – Lennart Nov 10 '17 at 10:10
  • If `[ImplementPropertychanged]` is obsolete it's replacement must be `[AddINotifyPropertyChangedInterface]` – PandaWood Jul 27 '21 at 12:50
-1

It seems you are not notifying that the text has changed at all, therefore the view has no clue there is a new value!

Try this code (complete with property changed logic for your sanity) in substitute of the Text variable setter:

    public string Text
    {
        get => _text;
        set
        {
            OnPropertyChanged(nameof(Text));
            _text = value;
        }
    }
    private string _text;
    public event PropertyChangedEventHandler PropertyChanged;
    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

PLUS you need to update a section of your XAML to say:

<TextBlock Text="{Binding Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />