SelectedColor 绑定不会从 ColorPicker 更新到 Model

SelectedColor binding doesn't update from ColorPicker to Model

我有一个 WPF 应用程序,我需要允许更改外观(主要是背景和前景)。所以我将它们绑定到在 App.resources 中定义应用程序范围的动态资源。

我还决定在我的设置 window

中使用来自 wpftoolkit (v2.5.0) 的 ColorPicker

简化示例

App.xaml

<Application x:Class="WpfColors.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <SolidColorBrush x:Key="BgBrush" Color="Gray"/>
    </Application.Resources>
</Application>

MainWindow.xaml 带拾色器

<Window x:Class="WpfColors.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid Name="grdBrushes" 
                  Background="{DynamicResource ResourceKey=BgBrush}" 
                  AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Width="*" Header="Element" Binding="{Binding Path=Name}"/>

                <DataGridTemplateColumn Width="*" Header="Color">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <xctk:ColorPicker SelectedColor="{Binding Path=BrushColor, Mode=TwoWay}"
                                              AvailableColorsHeader="Available" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>        
    </Grid>
</Window>

MainWindow.cs

using System.Linq;
using System.Windows;
using System.Windows.Media;

namespace WpfColors
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var res = Application.Current.Resources;
            grdBrushes.ItemsSource = res.Keys.OfType<string>()
                .Select(resKey => new AppBrush(resKey, ((SolidColorBrush) res[resKey]).Color))
                .ToList();
        }
    }
}

刷模型

using System.ComponentModel;
using System.Windows;
using System.Windows.Media;

namespace WpfColors
{
    public class AppBrush : INotifyPropertyChanged
    {
        public AppBrush(string name, Color color)
        {
            Name = name;
            _brushColor = color;
        }

        public string Name { get; set; }

        private Color? _brushColor;
        public Color? BrushColor
        {
            get { return _brushColor; }
            set 
            {
                // BREAKPOINT
                _brushColor = value;
                if (_brushColor.HasValue)
                    Application.Current.Resources[Name] = new SolidColorBrush(_brushColor.Value);
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("BrushColor"));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

问题是当我 select 颜色时,AppBrush 中的 BREAKPOINT 没有命中。 BrushColor 绑定到 ColorPicker SelectedColor。如果我更改 BrushColorColorPicker 会更新。

这是 ColorPicker 错误还是我的错误? selection 更改后如何立即更新 App 画笔?

大概它会刷新源代码,但要么是在它失去焦点时,要么是明确地。利用

SelectedColor="{Binding Path=BrushColor, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"