tool tip content change getting applied to all elements

107 views Asked by At

I am creating an application in which I am creating grid splitters dynamically and applying style in the code. The style also sets the tooltip for these gridsplitters. I am using tool tip to display the width of controls and dynamically updating them when resized. When i am changing content of one tool tip using the DragDelta event it is getting applied to tool tips of all gridsplitters (all start displaying the same widths). Below is the code:

Style XAML:

<Style x:Key="VGS" TargetType="GridSplitter">
                <Setter Property="IsTabStop" Value="False" />
                <Setter Property="Width" Value="5"/>
                <Setter Property="VerticalAlignment" Value="Stretch"/>
                <Setter Property="Grid.RowSpan" Value="1"/>
                <Setter Property="Background">
                    <Setter.Value>
                        <SolidColorBrush Opacity="0"/>
                    </Setter.Value>
                </Setter>

                <Setter Property="ToolTip">
                    <Setter.Value>
                        <ToolTip MinWidth="75" BorderBrush="Black" HasDropShadow="False"  
                                 Placement="Top" PlacementRectangle="-70,-5,50,50" 
                                 HorizontalContentAlignment="Left" >
                        </ToolTip>
                    </Setter.Value>
                </Setter>
                <EventSetter Event="DragDelta" Handler="Drag_VerticalGridSplitter"/>
                <EventSetter Event="PreviewMouseLeftButtonUp" Handler="Update_TableColumnDimensions"/>
                <EventSetter Event="PreviewMouseLeftButtonDown" Handler="Show_ToolTip"/>

            </Style>

Method to create GridSplitter:

private GridSplitter Get_VerticalGridSplitter(int column)
    {
        GridSplitter gs = new GridSplitter();
        Grid.SetRow(gs, 0);
        Grid.SetColumn(gs, column);
        gs.ToolTip = "Width: 150";
        gs.Style = MainGrid.FindResource("VGS") as Style;

        return gs;
    }

Method for updating tool tip:

void Drag_VerticalGridSplitter(object sender, DragDeltaEventArgs e)
    {
        //Sets grid width as the grid is resized
        double newGridWidth = 0;

        foreach (ColumnDefinition columnDefinition in MainGrid.ColumnDefinitions)
            newGridWidth = newGridWidth + columnDefinition.Width.Value;

        MainGrid.Width = newGridWidth;

        //update border
        MainGridBorder.Width = MainGrid.Width + 2;

        GridSplitter gs = sender as GridSplitter;
        ToolTip tt = gs.ToolTip as ToolTip;

        tt.Content = "Width: " + MainGrid.ColumnDefinitions[Grid.GetColumn(gs)].Width;
        tt.PlacementTarget = gs;
        tt.IsOpen = true;
    }

I feel that when I am updating the content it is some how changing the style. Can anybody point out what is wrong here. Thanks

1

There are 1 answers

1
Xavier On BEST ANSWER

I suspect the issue has to do with how xaml resources work. Since you are creating a ToolTip instance inside of a style setter, you are likely only ever creating one ToolTip and assigning it to every GridSplitter that uses that style. You could try adding x:Shared="false" to your style to make it create new instances of the style each time it is applied, or you could try defining a separate style targeting the ToolTip type and using that directly.