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

时间:2016-05-05 11:12:48

标签: wpf c#-4.0

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

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

1 个答案:

答案 0 :(得分:0)

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

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

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