c# - 如何制作带注释的滚动条?

标签 c# wpf windows xaml

我正在使用 DataGrid 来显示一些信息。

我想为这个 DataGrid 添加带注释的 ScrollBar(以突出显示行,就像在 VS 中一样)

我的 App.xaml 中有这个 ControlTemplate。我在 "AnnotationCanvas" 中添加了一个矩形作为示例,但我的想法是自动填充此 Canvas 。

但是,我无法获得对此 Canvas 的引用。

我尝试了 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>

最佳答案

我发现我用来从 DataGrid 获取 ScrollBar 的 VisualTreeHelper 在控件未完全加载的情况下不起作用。 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++;
    }
}

关于c# - 如何制作带注释的滚动条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42157712/

相关文章:

C# winform 闪烁标签背景色延迟

c# - Resharper解释 "Possible multiple enumeration of IEnumerable"的示例代码

c# - 如何在 wpf 中检测多个按键按下 onkeydown 事件?

wpf - 设置 wpf 数据网格的样式

c# - 如何使用c#在wpf中捕获WindowsFormsHost的图像

c# - 为什么静态类的Type属性在C#中显示为 "abstract"?

c# - 如何从 Entity Framework 代码优先方法自动增加 oracle 表?

c++ - 在 C++ 中使用 IFileDialog 以编程方式预选择

c++ 重数据处理和分页

windows - 创建命名管道 (WCF) 所需的最低操作系统权限