1

我试图创造这样的东西 -

在此处输入图像描述

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

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

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

4

1 回答 1

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如果您需要,处理事件将作为一种练习。

于 2013-06-30T06:21:31.087 回答