2016-05-05 28 views
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#


+0

我可以問你爲什麼要這樣?這是控制的默認行爲,因爲選擇沒有改變 – Muds

+0

對於我的控制面臨一個小問題。例如:在Ms-有填充顏色。 – Vinothkumar

+0

所以,如果我不開火的事件,我怎麼關閉顏色選擇器的下拉按鈕。我知道它的顏色選擇器行爲。我問任何其他方式來實現我的要求。 – Vinothkumar

回答

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; 
    } 
} 
相關問題