現在、Fodyプロパティを使用してMVVMを使用してWPFプロジェクトを作成しようとしています。 PropertyChangedが期待通りに動作しない
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";
}
}
<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>
は基本的に私は、私はボタンをクリックしたときのTextBlockは、 "テスト" -Textを示したいと思います。 Click-Commandは実行されますが、TextBlockのテキストは変更されません。 TextBoxを最新の状態に保つローカルメモリとしてTextプロパティを使用して、後で値をmodel.textに送信してそこで使用できるようにします。しかし、私が現在コメントしているコードを使用する場合にのみ機能します。 fody weaverが私のために同じことをやろうとしているのではないのですか?(単にmodel.textを使う代わりに、別のprivate変数を作成するだけです)?
FodyWeavers.xmlにウィーバーを追加しましたか?また、PropertyChangedEventに空のガードデリゲートを置かずに試してみてください.Fodyがそれを上書きしたくないかもしれません。 – Lennart
あなたのコードはFodyのバージョン2.1.4を使ってうまく動作します。 – mm8