2010-11-12 50 views
6

我一直在試圖從工具箱中我的應用程序工作的新顏色拾取,沒有成功...新的擴展WPFToolkit顏色拾取

這裏是應該拿起窗口背景的顏色樣本代碼填充當前顏色,並在新的選擇,應該是背景顏色改變爲所選顏色...

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="100" Width="200" xmlns:extToolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended" 
     Name="Window" Background="blue"> 
    <Grid> 
     <extToolkit:ColorPicker Name="colorPicker1" 
           SelectedColor="{Binding ElementName=Window,Path=Background}" 
           CurrentColor="{Binding ElementName=Window,Path=Background}" /> 
    </Grid> 
</Window> 

這是我已經能夠找到的顏色拾取的文檔... http://elegantcode.com/2010/08/15/extended-wpf-toolkit-new-colorpicker-control/

回答

9

這裏的問題是Window.Background是一個Brush和SelectedColor,而CurrentColor是Color。你可以通過使用Converter來獲得這個功能。

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:extToolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended" 
     xmlns:local="clr-namespace:WpfApplication1" 
     Title="MainWindow" Height="100" Width="200" 
     Name="Window" Background="blue"> 
    <Window.Resources> 
     <local:BrushColorConverter x:Key="BrushColorConverter"/> 
    </Window.Resources> 
    <Grid> 
     <extToolkit:ColorPicker Name="colorPicker1" 
           SelectedColor="{Binding ElementName=Window, 
            Path=Background, 
            Converter={StaticResource BrushColorConverter}}" 
           CurrentColor="{Binding ElementName=Window, 
            Path=Background, 
            Converter={StaticResource BrushColorConverter}}" /> 
    </Grid> 
</Window> 

和轉換器

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows.Data; 
using System.Windows.Media; 

namespace WpfApplication1 
{ 
    public class BrushColorConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      SolidColorBrush brush = value as SolidColorBrush; 
      return brush.Color; 
     } 
     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      Color color = (Color)value; 
      return new SolidColorBrush(color); 
     } 
    } 
} 
+0

謝謝Meleak!你再次幫助我! – 2010-11-14 14:02:43

0

乍一看看起來很正確。嘗試在調試模式下運行您的應用程序,並觀察Visual Studio中的輸出窗口是否存在綁定錯誤。

4

轉換功能不工作對我來說,這最終的伎倆:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
{ 
    return new SolidColorBrush((Color)value); 
} 
0

使用的設置作爲中介。在Settings.settings中創建一個類型爲string的用戶作用域參數。將其命名爲BackColor1 然後爲控件和元素的背景創建綁定,將它們都設置爲相同的設置(如下所示)。優點是用戶獲得持久性設置。我想確切地說,我測試了它作爲網格行的背景,而不是一個窗口,但它應該工作相同。

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="100" Width="200" xmlns:extToolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended" 
    Name="Window" 
    Background="{Binding Source={x:Static p:Settings.Default}, Path=BackColor1, Mode=TwoWay}"> 
    <Grid> 
     <extToolkit:ColorPicker 
     SelectedColor="{Binding Source={x:Static p:Settings.Default}, Path=BackColor1, Mode=TwoWay}"/> 
    </Grid> 
</Window>