为什么双向绑定不会更新 UI 元素

本文关键字:更新 UI 元素 绑定 为什么 | 更新日期: 2023-09-27 17:56:31

非常简单

的事情,但我正在拔头发。

我正在使用 mvvm Light 创建一个视图模型,并且在视图中我还有一个绑定的属性。

private TimeSpan _eventDuration
public TimeSpan EventDuration
{
  get { return _eventDuration; }
  set 
    {
      _eventDuration = value;
      RaisePropertyChanged("EventDuration");
    }
}

非常简单,并不复杂。现在在 UI 中我有一个网格,在这个网格上我正在使用一个名为 RadTimeSpanPickertelerik控件。此控件绑定到上述属性,如下所示。

Value={Binding EventDuration, Mode=TwoWay}

现在,当我运行代码并在 UI 中更改 RadTimeSpanControl 的值时,我看到更改发生在 EventDuration 属性上。

我的问题是,如果我更改属性,UI 不会更新。

所以,只是为了澄清。UI,我将时间跨度更改为00:00:16(即 16 秒)。我看到事件持续时间属性更改。然后我执行EventDuration = TimeSpan.FromSeconds(0);,之后我看到该属性已设置,但 UI 仍然显示00:00:16

更新:问题已解决我将TimeSpanPicker设置为0:0:1(1秒)的最小值,因此当尝试为0秒分配时间跨度时,UI部分只是忽略了它。

为什么双向绑定不会更新 UI 元素

So, just to clarify. UI, I change the timespan to 00:00:16 (that's 16 seconds). I see the EventDuration property change.

也许是错别字?!事件持续时间 = 时间跨度.从秒 (0);

它似乎对我有用:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <StackPanel>
            <telerikInput:RadTimeSpanPicker Value="{Binding EventDuration, Mode=TwoWay}" x:Name="radTimeSpanPicker"/>
            <Button Command="{Binding SetToNow}" Content="Set To Now" />
        </StackPanel>
    </Grid>

法典

 public class MainViewModel : ViewModelBase
{
    public RelayCommand SetToNow { get; private set; }
    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public MainViewModel()
    {
        this.SetToNow = new RelayCommand(SetToNowAction);
    }
    private TimeSpan _eventDuration;
    public TimeSpan EventDuration
    {
      get { return _eventDuration; }
      set 
        {
          _eventDuration = value;
          RaisePropertyChanged("EventDuration");
        }
    }
    private void SetToNowAction()
    {
        this.EventDuration = TimeSpan.FromSeconds(0);
    }