WPF-自定义颜色组合框 [英] WPF - Custom Color ComboBox

查看:139
本文介绍了WPF-自定义颜色组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我一直在尝试创建一个自定义的颜色选择器,并使用其他一些示例并对其进行了修改,使其能够很好地工作.这是XAML:

Hi all,

I''ve been trying to create a custom color picker and have got it working fairly well using some other examples and amending them. Here''s the XAML:

<UserControl x:Class="Customcontrols.Colorpicker"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 

             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

             xmlns:sys="clr-namespace:System;assembly=mscorlib" Height="40" Width="200" Name="uccolorpicker"

             mc:Ignorable="d">
    <UserControl.Resources>
        
        <ResourceDictionary>
            <ObjectDataProvider MethodName="GetType" 

    ObjectType="{x:Type sys:Type}" x:Key="colorsTypeOdp">
                <ObjectDataProvider.MethodParameters>
                    <sys:String>System.Windows.Media.Colors, PresentationCore,
            Version=3.0.0.0, Culture=neutral, 
            PublicKeyToken=31bf3856ad364e35</sys:String>
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>
            <ObjectDataProvider ObjectInstance="{StaticResource colorsTypeOdp}"  

    MethodName="GetProperties" x:Key="colorPropertiesOdp">
            </ObjectDataProvider>

        </ResourceDictionary>
    </UserControl.Resources>
    <Grid>
        <ComboBox x:Name="superCombo" 

            Validation.ErrorTemplate="{StaticResource ComboBoxValidationErrorTamplate}"

            ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}" 

            SelectedValuePath="Name"

            SelectedValue="{Binding ElementName=uccolorpicker, Path=SelectedColor}" SelectionChanged="superCombo_SelectionChanged">
            <ComboBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel x:Name="wpColours" Width="{Binding ActualWidth,
                        ElementName=superCombo}" Orientation="Horizontal" ItemHeight="15"

                        ItemWidth="20" Margin="2">                        
                    </WrapPanel>
                    
                </ItemsPanelTemplate>
                
            </ComboBox.ItemsPanel>
        <ComboBox.ItemTemplate>
            <DataTemplate>                    
                        <Rectangle x:Name="rect" Fill="{Binding Path=Name}" Height="{Binding ActualHeight,
                        ElementName=superCombo}" Width="{Binding ActualWidth,
                        ElementName=superCombo}"

                        RadiusX="0" RadiusY="0"/>
                </DataTemplate>
        </ComboBox.ItemTemplate> 
            
        </ComboBox>
    </Grid>
</UserControl>



以及后面的代码:



And the code behind:

public partial class Colorpicker : UserControl
    {
        public Colorpicker()
        {
            InitializeComponent();
        }

        public Brush SelectedColor
        {
            get { return (Brush)GetValue(SelectedColorProperty); }
            set { SetValue(SelectedColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SelectedColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedColorProperty =
            DependencyProperty.Register("SelectedColor", typeof(Brush), typeof(Colorpicker), new UIPropertyMetadata(null));
}



现在,我无法理解的问题是,当我尝试将初始选择的值设置为后面代码中的颜色,以便在"rect"中显示正确的颜色时.如果我使用KnownColor枚举类型按以下方式进行操作,则效果很好:



Now the problem II can''t understand is when I try and set the initial selected value to a color in code behind so that it displays the correct color in "rect". If I do it as follows with a KnownColor enum type it works fine:

superCombo.superCombo.SelectedValue = KnownColor.Red;



但是,如果我尝试这样做:



But if I try and do that with say this:

superCombo.superCombo.SelectedValue = Colors.Plum;



它不起作用. SelectedValue保持为空.

哦,我也差点忘了这部分:



It doesn''t work. The SelectedValue stays as null.

Oooh, I almost forgot this part too:

<Application.Resources>
        <ControlTemplate x:Key="ComboBoxValidationErrorTamplate">
            <DockPanel>
                <Border BorderBrush="Transparent" BorderThickness="1">
                    <AdornedElementPlaceholder />
                </Border>
            </DockPanel>
        </ControlTemplate>
    </Application.Resources>



对此的任何帮助将不胜感激.为什么我可以使用KnownColor或SystemColor来设置初始值,而不能使用System.Windows.Media.Color来设置初始值?当itemssource是该类型的集合时?

问候,

Jib



Any help with this is muchly appreciated. Why can I use KnownColor or SystemColor to set the initial value, but not System.Windows.Media.Color? When the itemssource is a collection of that type?

Regards,

Jib

推荐答案

KnownColor,该枚举是WinForms特有的,不是将两种不同技术的不同颜色定义混合使用的好主意,除非您确实必须这样做案例在这里.
另一件事是,在代码中您必须将颜色转换为纯色笔刷,因为我们指的是笔刷.
KnownColor, that enumeration is very specific to WinForms, not a good idea to mix different color definitions of two different technologies unless you really have to which is not the case here.
Another thing is that in code you have to convert the color to a solid brush, since our are referring to brushes.


设法像这样成功地在代码上设置了它:

Managed to set it on code successfully like so:

PropertyInfo[] properties = typeof(Colors).GetProperties();
           int i = 0;
           foreach (PropertyInfo property in properties)
           {
               var color = property.GetValue(null, null);

               if(((System.Windows.Media.Color)color).A == Colors.PowderBlue.A &&
                      ((System.Windows.Media.Color)color).R == Colors.PowderBlue.R &&
                      ((System.Windows.Media.Color)color).G == Colors.PowderBlue.G &&
                      ((System.Windows.Media.Color)color).B == Colors.PowderBlue.B)
               {
                   superCombo.superCombo.SelectedIndex = i;
               }
               i++;
           }



感谢您的所有意见和建议,

臂架



Thanks for all your input and ideas,

Jib


这篇关于WPF-自定义颜色组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆