2015-11-17 105 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}" 
相關問題