本文介绍了SelectedColor绑定不会从ColorPicker更新为Model的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a WPF application where I need to allow to change appearance (backgrounds and foreground mostly). So i bind them to dynamic resources which defined application-wide in App.resources.

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

I also decided to use a ColorPicker from wpftoolkit (v2.5.0) in my Settings window

简化示例

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;
    }
}

问题是当我选择颜色时,未击中AppBrush中的 BREAKPOINT . BrushColor绑定到ColorPicker SelectedColor.如果我更改BrushColor,则会更新ColorPicker.

The problem is that BREAKPOINT in AppBrush is not hit when i select color. BrushColor is bound to ColorPicker SelectedColor. If i change BrushColor, ColorPicker is updated.

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

is it a ColorPicker bug or my? how can I update App brushes immediately after selection changed?

推荐答案

大概可以刷新源代码,但是当它失去焦点或显式时会刷新.利用

Presumably it refreshes source but either when it looses focus or explicitly. Make use of

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

这篇关于SelectedColor绑定不会从ColorPicker更新为Model的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 03:32