2017-02-10 45 views
0

我正在使用DataGrid來顯示一些信息。如何製作帶註釋的滾動條?

我想有這個DataGrid(突出的線條,就像在VS)

我有這個ControlTemplate在我的App.xaml一個註釋ScrollBar。作爲示例,我在「AnnotationCanvas」中添加了一個矩形,但想法是自動填充此畫布。

但是,我無法獲得對此畫布的參考。

我試過dataGrid.FindName("AnnotationCanvas")dataGrid.FindResource("AnnotationCanvas"),但沒有成功。

我該怎麼做?提前致謝。

<ControlTemplate x:Key="VerticalScrollBar" TargetType="{x:Type ScrollBar}"> 
    <Grid x:Name="grid"> 
    <Grid.RowDefinitions> 
     <RowDefinition MaxHeight="18" /> 
     <RowDefinition Height="0.00001*" /> 
     <RowDefinition MaxHeight="18" /> 
    </Grid.RowDefinitions> 
    <Border Grid.RowSpan="3" 
      CornerRadius="2" 
      Background="#F0F0F0" /> 

    <Canvas x:Name="AnnotationCanvas" 
      Grid.Row="1"> 
     <!--ANNOTATIONS GO HERE--> 
     <Rectangle Width="3" 
       Height="30" 
       Fill="Red" /> 
    </Canvas> 

    <RepeatButton Grid.Row="0" 
        Style="{StaticResource ScrollBarLineButton}" 
        Height="18" 
        Command="ScrollBar.LineUpCommand" 
        Content="M 0 4 L 8 4 L 4 0 Z" /> 
    <Track Name="PART_Track" 
      Grid.Row="1" 
      IsDirectionReversed="true"> 
     <Track.DecreaseRepeatButton> 
     <RepeatButton Style="{StaticResource ScrollBarPageButton}" 
         Command="ScrollBar.PageUpCommand" /> 
     </Track.DecreaseRepeatButton> 
     <Track.Thumb> 
     <Thumb Style="{StaticResource ScrollBarThumb}" 
       Margin="1,0,1,0" 
       Background="{StaticResource HorizontalNormalBrush}" 
       BorderBrush="{StaticResource HorizontalNormalBorderBrush}" /> 
     </Track.Thumb> 
     <Track.IncreaseRepeatButton> 
     <RepeatButton Style="{StaticResource ScrollBarPageButton}" 
         Command="ScrollBar.PageDownCommand" /> 
     </Track.IncreaseRepeatButton> 
    </Track> 
    <RepeatButton Grid.Row="3" 
        Style="{StaticResource ScrollBarLineButton}" 
        Height="18" 
        Command="ScrollBar.LineDownCommand" 
        Content="M 0 0 L 4 4 L 8 0 Z" /> 
    </Grid> 
</ControlTemplate> 
+0

我刪除的代碼片段標記,因爲你的例子不是Web的代碼片段,而是一個簡單的XAML標記。 – dymanoid

+0

http://stackoverflow.com/questions/8126700/how-do-i-access-an-element-of-a-control-template-from-within-code-behind – ASh

+0

我看到了這個答案,但dataGrid.Template .FindName(「AnnotationCanvas」,dataGrid)爲我返回null –

回答

0

我發現我使用VisualTreeHelper從DataGrid到ScrollBar將無法正常工作,控件未完全加載。 InitializeComponent()是不夠的,我不得不等待Loaded事件觸發。

這裏是我的代碼(工作):

private Canvas annotationCanvas; 
private void Loaded(object sender, RoutedEventArgs e) { 

    ScrollBar scrollBar = GetVisualChild<ScrollBar>(table); 
    annotationCanvas = (Canvas)scrollBar.Template.FindName("AnnotationCanvas", scrollBar); 

    UpdateAnnotations(); 
} 

private static T GetVisualChild<T>(DependencyObject parent) where T : Visual { 
    T child = default(T); 

    int numVisuals = VisualTreeHelper.GetChildrenCount(parent); 
    for (int i = 0; i < numVisuals; i++) { 
     Visual v = (Visual)VisualTreeHelper.GetChild(parent, i); 
     child = v as T; 
     if (child == null) { 
      child = GetVisualChild<T>(v); 
     } 
     if (child != null) { 
      break; 
     } 
    } 
    return child; 
} 

// Fill the Canvas with horizontal markers. Can be optimized. 
private void UpdateAnnotations() { 
    if (annotationCanvas == null) 
     return; 

    annotationCanvas.Children.Clear(); 

    int i = 0; 
    double m = items.Count; 
    double height = table.ActualHeight; 

    foreach (Item item in items) { 
     if (item.someCondition) { 
      int p = (int)(height * i/m); 
      annotationCanvas.Children.Add(new Line() { X1 = 0, Y1 = p, X2 = 30, Y2 = p, StrokeThickness = 1, Stroke = Brushes.Red }); 
     } 
     i++; 
    } 
}