2015-11-17 103 views
2

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

我还决定在我的设置窗口

setting window with colorPicker

简化的示例使用ColorPickerwpftoolkit(V2.5.0)

的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当我选择的颜色不打。 BrushColor绑定到ColorPicker SelectedColor。如果我更改BrushColor,则更新ColorPicker

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

+0

尝试设置 SelectedColor = “{绑定路径= BrushColor,模式=双向,UpdateSourceTrigger = PropertChanged}” – Maximus

回答

3

大概它刷新源,但无论是当它失去焦点或明确。利用

SelectedColor="{Binding Path=BrushColor, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
相关问题