2017-09-11 17 views
0

ColumnSeries 인 WPF Toolkit 차트가 있습니다. ColumnSeries는 일련의 모든 컬럼에 영향을 미치는 뒤에 코드에서 SelectionChanged 이벤트 및 기본 스타일이(WPF Toolkit) 열 시리즈의 각 DataPoint에 대한 색 설정

<chartingToolkit:ColumnSeries DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding ColumnValues}" IsSelectionEnabled="True" SelectionChanged="ColumnSeries_SelectionChanged"> 
    <chartingToolkit:ColumnSeries.DataPointStyle> 
     <Style TargetType="chartingToolkit:ColumnDataPoint"> 
      <Setter Property="Background" Value="{StaticResource HeaderForegroundBrush}" /> 
     </Style> 
    </chartingToolkit:ColumnSeries.DataPointStyle> 
</chartingToolkit:ColumnSeries> 
나는 하나의 스타일을 코드 숨김에서 전체 ColumnSeries가의 스타일을 변경할 수 있지만, 어떻게 할 수

열이 변경 되었습니까? 이것이 가능합니까?

private void ColumnSeries_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    Style lineStyle = new Style { TargetType = typeof(ColumnDataPoint) }; 
    lineStyle.Setters.Add(new Setter(ColumnDataPoint.BackgroundProperty, (Brush)Application.Current.Resources["Line1Brush"])); 
    ((ColumnSeries)sender).DataPointStyle = lineStyle; 
} 

답변

1

당신은 시각적 트리에서 ColumnDataPoint 요소를 찾기 위해 도우미 메서드를 사용하고 개별 요소의 원하는 특성, 예컨대 :

private void ColumnSeries_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    ColumnSeries cs = sender as ColumnSeries; 
    IEnumerable<ColumnDataPoint> columns = FindVisualChildren<ColumnDataPoint>(cs); 
    foreach (var column in columns) 
    { 
     if (column.DataContext == e.AddedItems[0]) //change the background of the selected one 
     { 
      column.Background = Brushes.DarkBlue; 
      break; 
     } 
    } 

} 

private static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject 
{ 
    if (depObj != null) 
    { 
     int NbChild = VisualTreeHelper.GetChildrenCount(depObj); 

     for (int i = 0; i < NbChild; i++) 
     { 
      DependencyObject child = VisualTreeHelper.GetChild(depObj, i); 

      if (child != null && child is T) 
      { 
       yield return (T)child; 
      } 

      foreach (T childNiv2 in FindVisualChildren<T>(child)) 
      { 
       yield return childNiv2; 
      } 
     } 
    } 
} 
을 설정할 수 있습니다