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

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

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

简化的例子

应用程式

<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

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

最佳答案

大概它刷新了源,但是当它失去焦点或显式时。利用

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

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

10-09 16:39