更新控件属性会破坏OneWay绑定吗?

时间:2018-08-15 06:49:16

标签: c# wpf binding user-controls dependency-properties

考虑这个非常简单的示例,其中有一个这样的UserControl:

UserControl XAML:

<UserControl x:Class="BindingTest.SomeControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             x:Name="SomeControlElement">
    <Grid>
        <TextBlock Text="{Binding ElementName=SomeControlElement, Path=Counter}" />
    </Grid>
</UserControl>

后面的代码:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
namespace BindingTest
{ 
    public partial class SomeControl : UserControl
    {
        public SomeControl()
        {
            InitializeComponent();

            var timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 5);
            timer.Tick += (s, e) => Counter = Counter + 1;
            timer.Start();
        }

        public int Counter
        {
            get { return (int)GetValue(CounterProperty); }
            set { SetValue(CounterProperty, value); }
        }
        public static readonly DependencyProperty CounterProperty = DependencyProperty.Register(nameof(Counter), typeof(int), typeof(SomeControl), new PropertyMetadata(0));
    }
}

因此,控件仅显示一个TextBlock,并且每5秒递增一次Counter。然后我当然有一个消费者:

MainWindow XAML:

<Window x:Class="BindingTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:BindingTest"
        x:Name="MainWindowName" Width="200" Height="300">
    <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
        <StackPanel>
            <local:SomeControl Counter="{Binding ElementName=MainWindowName, Path=SomeSource, Mode=OneWay}" />
            <local:SomeControl Counter="{Binding ElementName=MainWindowName, Path=SomeSource, Mode=TwoWay}" />
        </StackPanel>
    </Grid>
</Window>

最后是后面的主要代码:

using System;
using System.Windows;
using System.ComponentModel;
using System.Windows.Threading;
namespace BindingTest
{
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            var timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 1);
            timer.Tick += (s, e) => SomeSource = SomeSource + 1;
            timer.Start();
        }

        private int someSource;
        public int SomeSource
        {
            get => someSource;
            set
            {
                if (someSource != value)
                {
                    someSource = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SomeSource)));
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}

是的,因此主代码后面有一个计数器,该计数器每秒更新一个属性。 XAML具有2个UserControl实例。一种具有 OneWay 绑定的,另一种具有 TwoWay 绑定的。

我在这里看到的是,当“ SomeControl.cs”中的Counter更新时,第一个UserControl(OneWay)的绑定被打破。带有TwoWay的版本会不断更新。

这是设计使然(为什么)吗?更重要的是,如果我需要更新UserControls中的属性,那么在我的示例中该怎么做-为了支持OneWay绑定?请注意,在此示例中,我对TwoWay绑定真的不感兴趣,因为它会更新“ MySource”,这不是我想要的!

谢谢。

1 个答案:

答案 0 :(得分:1)

这是设计使然。当您为依赖项属性分配所谓的local value时,将替换先前分配的OneWay绑定。 TwoWay绑定保持活动状态并更新其source属性。

但是,有一种解决方法。不要设置本地值,而要设置“当前值”。替换

timer.Tick += (s, e) => Counter = Counter + 1;

使用

timer.Tick += (s, e) => SetCurrentValue(CounterProperty, Counter + 1);