2010-11-12 10 views
6

que he estado tratando de conseguir la nueva colorpicker de la caja de herramientas de trabajo en mi aplicación, sin éxito ...Nueva Extended WPFToolkit ColorPicker

Aquí es código de ejemplo que se supone que debe recoger el color del fondo de la ventana para llenar de color actual, y sobre nueva selección, se supone que cambiar el color de fondo al color seleccionado ...

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

Esta es toda la documentación que he podido localizar en la colorpicker ... http://elegantcode.com/2010/08/15/extended-wpf-toolkit-new-colorpicker-control/

Respuesta

9

El problema aquí es que Window.Background es un pincel y SelectedColor y CurrentColor es Color. Puedes hacer que esto funcione usando un convertidor.

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

y el convertidor

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

Gracias Meleak! ¡Me has ayudado una vez más! –

0

Eso se ve a primera vista. Intente ejecutar su aplicación en modo de depuración y mire la ventana Salida en Visual Studio para ver si hay errores de enlace.

2

Gracias Meleak. Esta pregunta también ha sido respondida en el discussions.

También se ha actualizado recientemente ColorPicker. Check it out.

4

la función de conversión no funcionaba para mí, con el tiempo esto hizo el truco:

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

utilizar los ajustes como intermediario. En sus Configuraciones, cree un parámetro de alcance de usuario de tipo cadena. Nómbrelo BackColor1 A continuación, cree enlaces para el control y para el fondo del elemento, ambos con la misma configuración (como se muestra a continuación). La ventaja es que el usuario obtiene una configuración persistente. Quiero precisar que lo probé como fondo de una fila de cuadrícula, no como una ventana, pero debería funcionar igual.

<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> 
Cuestiones relacionadas