wpf - SelectedColor 绑定(bind)不会从 ColorPicker 更新到 Model

标签 wpf binding wpftoolkit color-picker dynamicresource

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

我还决定使用 ColorPicker来自 wpftoolkit (v2.5.0) 在我的设置窗口中

setting window with colorPicker

简化示例

应用程序.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;
    }
}

问题是 断点 当我选择颜色时,AppBrush 中没有被击中。 BrushColor绑定(bind)到 ColorPicker SelectedColor .如果我改变 BrushColor , ColorPicker已更新。

是 ColorPicker 错误还是我的?选择更改后如何立即更新应用画笔?

最佳答案

大概它会刷新源代码,但是当它失去焦点或明确时。利用

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

关于wpf - SelectedColor 绑定(bind)不会从 ColorPicker 更新到 Model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33755506/

相关文章:

c# - WPF 文本 block /直接通过 Inlines.Add() 设置粗体、斜体、下划线标签

wpf - 找不到样式

c# - 从 View 到 ViewModel 的 WPF 事件绑定(bind)?

c# - 如何从我的绑定(bind)中引用其他框架?

wpf - 将Office 2007主题应用于WPF应用程序

wpf - 无法从程序集 PresentationFramework 加载类型 'System.Windows.Controls.Primitives.MultiSelector'

c# - WPF System.ComponentModel.Win32Exception (0x80004005) : Invalid window handle

wpf - 带有 DatePicker 的 DataGridTemplateColumn 需要单击三下才能编辑日期

wpf - 在上下文菜单命令参数 mvvm 中获取行

c# - CheckComboBox ValueMemberPath 不起作用