您是否可以将事件设置器绑定到数据上下文的事件?

时间:2013-06-29 17:12:40

标签: wpf propertychanged eventsetter

我试图创造这样的东西 -

enter image description here

我有一个可观察到的积分集合。每个点都有一个位置和一个颜色。当任何点位置或颜色发生变化(它们实现通知更改)时,我想“重新绘制”背景渐变。目前我有一个itemscontrol,我将滑块绑定到点位置,最初绘制渐变。现在,我想知道当'point'上的propertychanged事件触发时,我可以在视图后面的代码中调用函数,以便我可以重绘渐变。我想知道是否可以某种方式使用事件设定器?

虽然我可以在代码后面进行propertychanged事件订阅,但是我想在XAML中进行吗?

请注意:我特别想采用这种方法在代码中手动重新绘制其他原因,所以如果我能得到上面特定问题的答案而不是替代解决方案,请。

1 个答案:

答案 0 :(得分:1)

我猜你可以创建一个附加属性来订阅PropertyChanged属性值的DataContext事件。

public static class Props
{
    public static DependencyProperty OnPropertyChangedProperty = DependencyProperty.RegisterAttached(
        "OnPropertyChanged", typeof(PropertyChangedEventHandler), typeof(Props),
        new PropertyMetadata(OnPropertyChangedPropertyChanged));

    public static PropertyChangedEventHandler GetOnPropertyChanged (DependencyObject d)
    {
        return (PropertyChangedEventHandler)d.GetValue(OnPropertyChangedProperty);
    }

    public static void SetOnPropertyChanged (DependencyObject d, PropertyChangedEventHandler value)
    {
        d.SetValue(OnPropertyChangedProperty, value);
    }

    private static void OnPropertyChangedPropertyChanged (DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var inpc = (INotifyPropertyChanged)((FrameworkElement)d).DataContext;
        if (inpc == null)
            throw new ArgumentException("DataContext of the framework element must not be null.");
        var oldChanged = (PropertyChangedEventHandler)e.OldValue;
        if (oldChanged != null)
            inpc.PropertyChanged -= oldChanged;
        var newChanged = (PropertyChangedEventHandler)e.NewValue;
        if (newChanged != null)
            inpc.PropertyChanged += newChanged;
    }
}

用法:

<Window x:Class="So17382721PropertyChangedXaml.MainWindow" x:Name="root"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:So17382721PropertyChangedXaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate DataType="{x:Type local:Foo}">
            <!-- Here, we subscribe to DataContext.PropertyChanged;
                 handler is defined in the MainWindow class -->
            <Grid local:Props.OnPropertyChanged="{Binding FooPropertyChanged, ElementName=root}">
                <TextBox Text="{Binding Bar, UpdateSourceTrigger=PropertyChanged}"/>
            </Grid>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <ItemsControl ItemsSource="{Binding Foos, ElementName=root}"/>
    </Grid>
</Window>

代码隐藏:

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;

namespace So17382721PropertyChangedXaml
{
    public partial class MainWindow
    {
        public ObservableCollection<Foo> Foos { get; private set; }

        public MainWindow ()
        {
            Foos = new ObservableCollection<Foo> {
                new Foo { Bar = "1" },
                new Foo { Bar = "2" },
                new Foo { Bar = "3" },
            };
            InitializeComponent();
        }

        private void OnFooPropertyChanged (object sender, PropertyChangedEventArgs e)
        {
            MessageBox.Show(this, string.Format("{0} of {1} changed.", e.PropertyName, sender));
        }

        // Subscribing to non-RoutedEvents in XAML is not straightforward, but we can define a property
        public PropertyChangedEventHandler FooPropertyChanged
        {
            get { return OnFooPropertyChanged; }
        }
    }

    public class Foo : INotifyPropertyChanged
    {
        private string _bar;
        public string Bar
        {
            get { return _bar; }
            set
            {
                _bar = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged ([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

注意:附加属性Props.OnPropertyChanged期望DataContext在生命周期内不会更改,并且已经指定。如果您需要,处理DataContextChanged事件将被视为一个exircize。

相关问题