1
最初(完全公開)、私はC#の初心者です。私は既にstackoverflowの上で何十もの同様の質問と例を見直しましたが、私が間違っていることに私のファインダーをまだ置くことはできません。私はVS 2017 WPFappプロジェクトを開始しました。 OnStartupを変更して、手動でウィンドウをロードすることができました。値をインクリメントするタイマーを持つクラスを作成し、次にINotifyPropertyChangedを使用してMainWindowのTextBoxを更新します。UpdateSourceTrigger = PropertyChangedでWPFを更新する際に問題が発生しました
WPFウィンドウが正常に読み込まれます。 TextBlockは値5で始まるので、バインディングは少なくとも自分のクラスと値を指しています。タイマーイベントで値が更新され、NotifyPropertyChangedが発生してもTextBlockは変更されないことが出力に示されます。
私の唯一の考えは、TextBlockが私のUpdateWPFクラスの間違ったインスタンスにリンクされているということですが、私はそれがどうなるかはわかりません。
ありがとうございました! (実際には単なるテキストブロックを落とし、バインディングを設定)
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBlock HorizontalAlignment="Left" Height="50" Margin="139,123,0,0" TextWrapping="Wrap" Text="{Binding Path=GetValue,Mode=OneTime,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="215"/>
</Grid>
</Window>
MainWindow.xaml.csは(なかったの:ここ
は私のコードは...
MainWindow.xamlですこのコードでは、すべての)
namespace WpfApp1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
を変更 App.xaml(ちょうどStartupUriをコメントアウト)
<Application x:Class="WpfApp1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1">
<!--StartupUri="MainWindow.xaml"-->
<Application.Resources>
</Application.Resources>
</Application>
App.xaml.cs(肉とジャガイモ)
namespace WpfApp1
{
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
UpdateWPF uWPF = new UpdateWPF();
MainWindow w = new MainWindow();
w.DataContext = uWPF;
w.Show();
}
public class UpdateWPF : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
Console.WriteLine("NotifyPropertyChanged Fired.");
}
private int value = 5;
public UpdateWPF()
{
Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 1000;
aTimer.Enabled = true;
}
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
value++;
Console.WriteLine("Timer Event Fired. New Value is " + value.ToString());
NotifyPropertyChanged(nameof(GetValue));
}
public string GetValue => (value.ToString());
}
}
}
出力
Timer Event Fired. New Value is 6
NotifyPropertyChanged Fired.
Timer Event Fired. New Value is 7
NotifyPropertyChanged Fired.
Timer Event Fired. New Value is 8
NotifyPropertyChanged Fired.
Timer Event Fired. New Value is 9
NotifyPropertyChanged Fired.
Timer Event Fired. New Value is 10
NotifyPropertyChanged Fired.
Timer Event Fired. New Value is 11
NotifyPropertyChanged Fired.
'Mode = OneTime'予想される動作です。それを設定する必要はありません。 'UpdateSourceTrigger = PropertyChanged'は' TextBlock.Text'のために役に立たない設定です – ASh
これはすべて....... = Mode = OneTimeはMode = OneWayであるはずです! – lumberajackshaw
それを答えに入れて、私は答えに印をつけます。 – lumberajackshaw