How do you make the style use of the data from your counter?



  • How do you make the style use of the data from your counter?

        <DataGrid AutoGenerateColumns="False"
                  ColumnWidth="100">
            <DataGrid.Resources>
                <Style TargetType="DataGridColumnHeader">
                    <Setter Property="ToolTip" Value="{Binding //тут нужно привязаться к ToolTipText}"/>
                </Style>
            </DataGrid.Resources>
            <DataGrid.Columns>
                <local:DataGridTextColumnWithHeader   
                                        Header="!hello"
                                        ToolTipText="hello!"
                                        />
            </DataGrid.Columns>
        </DataGrid>
    

    Implementation DataGridTextColumnWithHeader:

     public class DataGridTextColumnWithHeader : DataGridTextColumn
    {
        public string ToolTipText
        {
            get { return (string)GetValue(ToolTipTextProperty); }
            set { SetValue(ToolTipTextProperty, value); }
        }
    
    public static readonly DependencyProperty ToolTipTextProperty =
        DependencyProperty.Register("ToolTipText", typeof(string), typeof(DataGridTextColumnWithHeader));
    

    }



  • Okay, that's not really easy, because Column- objects DataGrid is not part of a visual tree, and it's so easy not to reach.

    But in your case, a name can be given in the column and a name can be reached:

    <DataGrid AutoGenerateColumns="False" ColumnWidth="100">
        <DataGrid.Resources>
            <Style TargetType="DataGridColumnHeader">
                <Setter Property="ToolTip"
                        Value="{Binding ToolTipText, ElementName=ColumnWithHeader}"/>
            </Style>
        </DataGrid.Resources>
        <DataGrid.Columns>
            <local:DataGridTextColumnWithHeader x:Name="ColumnWithHeader"
                                Header="!hello"
                                ToolTipText="hello!"
                                />
        </DataGrid.Columns>
    </DataGrid>
    

    If you don't want to do the style for each column, you can use the code behind:

    <DataGrid AutoGenerateColumns="False" ColumnWidth="100" Loaded="OnDGLoaded" Name="DG">
        <DataGrid.Columns>
            <local:DataGridTextColumnWithHeader Header="!hello" ToolTipText="hello!"/>
        </DataGrid.Columns>
    </DataGrid>
    
    void OnDGLoaded(object sender, RoutedEventArgs e)
    {
        foreach (var header in GetVisualChildrenOfType<DataGridColumnHeader>(DG))
        {
            DataGridTextColumnWithHeader column = header.Column as DataGridTextColumnWithHeader;
            if (column != null)
                header.ToolTip = column.ToolTipText;
        }
    }
    

    IEnumerable<T> GetVisualChildrenOfType<T>(FrameworkElement fe) where T : FrameworkElement
    {
    T t = fe as T;
    if (t != null)
    yield return t;
    int count = VisualTreeHelper.GetChildrenCount(fe);
    for (int i = 0; i < count; i++)
    {
    FrameworkElement child = VisualTreeHelper.GetChild(fe, i) as FrameworkElement;
    if (child != null)
    {
    foreach (var r in GetVisualChildrenOfType<T>(child))
    yield return r;
    }
    }
    }

    It'll only take the tultip once when it's loaded. If it's not enough, you can have a manual connection, both of your objects in the cycle. OnDGLoaded Yes.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2