当我选择了颜色时,如何挂钩变色事件?

时间:2021-09-03 15:03:19

I have color picker. If I selected different colors means it is fire color changed event. But I select color is already selected color, the Color changed event is not fired. So how can I achieve this requirement or how can hook event for when select color is already selected.

我有颜色选择器。如果我选择不同的颜色意味着它是火色变化事件。但是我选择的颜色已经是选中的颜色,Color更改的事件不会被触发。那么我该如何实现这个要求,或者如何在选择了颜色时挂钩事件。

Xaml:

XAML:

<system:SplitButton x:Name="Font_FontColor"  Height="24" DataContext="{Binding ElementName=Font_FontColorPicker}">
<system:ColorPickerPalette x:Name="Font_FontColorPicker" system:SkinStorage.VisualStyle="Metro"
                                                                       BlackWhiteVisibility="Both"
                                                                       IsExpanded="True"
                                                                       MoreColorOptionVisibility="Collapsed"/>

C#

C#


1 个解决方案

#1


0  

it is a control behavior. so can make the code for achieve this by below codes

这是一种控制行为。所以可以通过下面的代码来实现这个代码

XAML:

XAML:

<syncfusion:SplitButton x:Name="Font_FontColor" Height="24" 
    DataContext="{Binding ElementName=Font_FontColorPicker}">
<syncfusion:ColorPickerPalette x:Name="Font_FontColorPicker" 
    syncfusion:SkinStorage.VisualStyle="Metro"
    BlackWhiteVisibility="Both" IsExpanded="True" MoreColorOptionVisibility="Collapsed" 
    Color="Red" />

C#:

C#:

public partial class MainWindow : Window
{
    bool CanHookEvents = true;

    public MainWindow()
    {
        InitializeComponent();
        Font_FontColorPicker.ColorChanged += Font_FontColorPicker_ColorChanged;
        //Font_FontColor.IsDropDownOpenChanged += Font_FontColor_IsDropDownOpenChanged;
        Font_FontColorPicker.Loaded += Font_FontColorPicker_Loaded;
    }

    private void Font_FontColorPicker_Loaded(object sender, RoutedEventArgs e)
    {
        if (CanHookEvents)
        {
            foreach (ColorGroupItem item in FindVisualChildrenOfType<ColorGroupItem>(Font_FontColorPicker))
            {
                if (item != null)
                {
                    item.PreviewMouseLeftButtonDown += item_PreviewMouseLeftButtonDown;
                }
            }
        }
    }
    void item_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if(Font_FontColorPicker.Color.Equals((((sender as ColorGroupItem).Color) as SolidColorBrush).Color))
        {
            // I have closed dropdown. Do your stuff here
            Font_FontColor.IsDropDownOpen = false;
        }
    }

    public static IEnumerable<T> FindVisualChildrenOfType<T>(DependencyObject parent)
   where T : DependencyObject
    {
        List<T> foundChildren = new List<T>();
        int childCount = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < childCount; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            T childType = child as T;
            if (childType == null)
            {
                foreach (var other in FindVisualChildrenOfType<T>(child))
                    yield return other;
            }
            else
            {
                yield return (T)child;
            }
        }
    }

    void Font_FontColorPicker_ColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Font_FontColor.IsDropDownOpen = false;
    }
}

#1


0  

it is a control behavior. so can make the code for achieve this by below codes

这是一种控制行为。所以可以通过下面的代码来实现这个代码

XAML:

XAML:

<syncfusion:SplitButton x:Name="Font_FontColor" Height="24" 
    DataContext="{Binding ElementName=Font_FontColorPicker}">
<syncfusion:ColorPickerPalette x:Name="Font_FontColorPicker" 
    syncfusion:SkinStorage.VisualStyle="Metro"
    BlackWhiteVisibility="Both" IsExpanded="True" MoreColorOptionVisibility="Collapsed" 
    Color="Red" />

C#:

C#:

public partial class MainWindow : Window
{
    bool CanHookEvents = true;

    public MainWindow()
    {
        InitializeComponent();
        Font_FontColorPicker.ColorChanged += Font_FontColorPicker_ColorChanged;
        //Font_FontColor.IsDropDownOpenChanged += Font_FontColor_IsDropDownOpenChanged;
        Font_FontColorPicker.Loaded += Font_FontColorPicker_Loaded;
    }

    private void Font_FontColorPicker_Loaded(object sender, RoutedEventArgs e)
    {
        if (CanHookEvents)
        {
            foreach (ColorGroupItem item in FindVisualChildrenOfType<ColorGroupItem>(Font_FontColorPicker))
            {
                if (item != null)
                {
                    item.PreviewMouseLeftButtonDown += item_PreviewMouseLeftButtonDown;
                }
            }
        }
    }
    void item_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if(Font_FontColorPicker.Color.Equals((((sender as ColorGroupItem).Color) as SolidColorBrush).Color))
        {
            // I have closed dropdown. Do your stuff here
            Font_FontColor.IsDropDownOpen = false;
        }
    }

    public static IEnumerable<T> FindVisualChildrenOfType<T>(DependencyObject parent)
   where T : DependencyObject
    {
        List<T> foundChildren = new List<T>();
        int childCount = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < childCount; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            T childType = child as T;
            if (childType == null)
            {
                foreach (var other in FindVisualChildrenOfType<T>(child))
                    yield return other;
            }
            else
            {
                yield return (T)child;
            }
        }
    }

    void Font_FontColorPicker_ColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Font_FontColor.IsDropDownOpen = false;
    }
}