【问题标题】:New Extended WPFToolkit ColorPicker新的扩展 WPFToolkit 颜色选择器
【发布时间】:2010-11-12 00:10:37
【问题描述】:

我一直在尝试让工具包中的新颜色选择器在我的应用程序中运行,但没有成功...

这里是示例代码,它应该获取窗口背景的颜色以填充当前颜色,并且在新选择时,应该将背景颜色更改为所选颜色...

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

【问题讨论】:

    标签: wpf controls


    【解决方案1】:

    这里的问题是 Window.Background 是画笔,而 SelectedColor 和 CurrentColor 是颜色。您可以使用转换器来实现它。

    <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);
            }
        }
    }
    

    【讨论】:

    • 感谢梅莱克!你又帮了我一次!
    【解决方案2】:

    转换功能对我不起作用,最终成功了:

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

    【讨论】:

    • 当我尝试从 ColorPicker 设置所选文本的颜色时,您的建议解决了我在以下代码行中的类似问题:myRichTxtBox.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, myColorPicker1.SelectedColor.Value); Error 是 @ 987654323@。我用new SolidColorBrush(...)包围了myColorPicker1.SelectedColor.Value,它解决了这个问题。 谢谢你来救我。
    【解决方案3】:

    感谢梅莱克。这个问题在discussions也有解答。

    最近也更新了 ColorPicker。 Check it out.

    【讨论】:

      【解决方案4】:

      乍一看,这似乎是正确的。尝试在调试模式下运行您的应用,并观察 Visual Studio 中的“输出”窗口是否存在绑定错误。

      【讨论】:

        【解决方案5】:

        使用设置作为中介。在您的 Settings.settings 中创建字符串类型的用户范围参数。将其命名为 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>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-17
          • 2013-02-11
          • 2018-10-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多