gpt4 book ai didi

c# - 简单的数据绑定(bind)不起作用

转载 作者:行者123 更新时间:2023-11-30 14:02:59 24 4
gpt4 key购买 nike

我是 WPF 的新手,我正在尝试制作一个简单的应用程序,一个秒表。如果我不进行数据绑定(bind),它工作正常。这是我的 XAML。

<Window x:Class="StopWatch.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:StopWatch"
Title="MainWindow" Height="318" Width="233">
<Window.Resources>
<s:StopWatchViewModel x:Key="swViewModel" x:Name="swViewModel"></s:StopWatchViewModel>
</Window.Resources>
<Grid DataContext="{StaticResource swViewModel}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="128*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="1" Height="49" HorizontalAlignment="Left" Margin="42,50,0,0" Name="txtTime" Text="{Binding Path=Message}" VerticalAlignment="Top" Width="147" FontSize="20" TextAlignment="Center" />
<Button Content="Start" Grid.Row="2" Height="23" HorizontalAlignment="Left" Margin="12,15,0,0" Name="startBtn" VerticalAlignment="Top" Width="58" Click="startBtn_Click" />
<Button Content="Stop" Grid.Row="2" Height="23" HorizontalAlignment="Left" Margin="76,15,0,0" Name="stopBtn" VerticalAlignment="Top" Width="58" Click="stopBtn_Click" />
<Button Content="Reset" Grid.Row="2" Height="23" HorizontalAlignment="Left" Margin="140,15,0,0" Name="resetBtn" VerticalAlignment="Top" Width="59"/>
</Grid>

这是主窗口中的代码

public partial class MainWindow : Window
{
private StopWatchViewModel stopwatch;

public MainWindow()
{
InitializeComponent();
stopwatch = new StopWatchViewModel();
}

private void startBtn_Click(object sender, RoutedEventArgs e)
{
stopwatch.Start();
}

private void stopBtn_Click(object sender, RoutedEventArgs e)
{
stopwatch.Stop();
}
}

这是 StopWatchViewModel.cs 中的代码

class StopWatchViewModel : INotifyPropertyChanged
{
private DispatcherTimer timer;
private Stopwatch stopwatch;
private string message;

public event PropertyChangedEventHandler PropertyChanged;

public string Message
{
get
{
return message;
}
set
{
if (message != value)
{
message = value;
OnPropertyChanged("Message");
}
}
}

public StopWatchViewModel()
{
timer = new DispatcherTimer();
stopwatch = new Stopwatch();
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
stopwatch.Reset();
}

public void Start()
{
stopwatch.Start();
}

public void Stop()
{
stopwatch.Stop();
}

private void timer_Tick(object sender, EventArgs e)
{
Message = stopwatch.Elapsed.ToString(); // Doesn't work.
// Message = "hello"; does not work too!
}

private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

}

我不知道我哪里错了。

编辑:我让它工作了。所以这是供任何人引用的工作代码。

XAML,把原来的改成这个

<Window x:Class="StopWatch.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:StopWatch"
Title="MainWindow" Height="318" Width="233">
<Grid> // partial code

在后面的代码中,根据 Erno 的建议更改构造函数。

public MainWindow()
{
InitializeComponent();
viewModel = new StopWatchViewModel();
this.DataContext = viewModel;
}

谢谢大家!

最佳答案

这里的问题是您没有任何机制让 WPF 知道您的属性已更新。基本上你在这里有两个选择:

  1. 使消息成为依赖属性。
  2. 实现 INotifyPropertyChanged以便让 GUI 知道消息何时更新。

确保您拥有获得 INotifyPropertyChanged 所需的所有部件工作,检查你是否做了所有这些:

  1. 定义事件 PropertyChanged .
  2. 设为私有(private)NotifyPropertyChanged引发事件的方法。此方法应采用字符串参数(属性名称)并像这样引发事件:PropertyChanged(this, new PropertyChangedEventArgs(<nameofproperty>) .制作此方法的原因是将空检查和调用细节放在一个地方。
  3. 在属性 setter 中,调用 NotifyPropertyChanged使用属性的正确名称(区分大小写)值更改后。

关于c# - 简单的数据绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4790711/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com