I have an app that scales it's UI and I want to scale the ToolTips with it. I have tried doing this:

<Style TargetType="{x:Type ToolTip}">
    <Setter Property="LayoutTransform" Value="{DynamicResource scaleTransf}"/>
    ...
</Style>

...where scaleTransf is a resource that I change via code:

Application.Current.Resources["scaleTransf"] = new ScaleTransform(...);

with:

<ScaleTransform x:Key="scaleTransf" ScaleX="1" ScaleY="1"/>

Most of the ToolTips do get scaled in size but some of them that are created by C# code don't get scaled. I've checked and it seems that I don't set their Style or LayoutTransform by code, so I don't really understand what is going wrong... Moreover, I have the impression that the above XAML code worked fine a few days ago. :(

Is there sth I can do to make it work all the time without setting the LayoutTransform in code-behind?

EDIT : The ToolTips that don't change scale are the ones that have become visible before.

EDIT1 : If I set the LayoutTransform of each ToolTip instance to scaleTransf in code behind using SetResourceReference() everything works fine. I don't understand why the Style doesn't work while it is supposed to do exactly the same for every ToolTip that gets created... Based on my limited knowledge of WPF I would call this a BUG!


EDIT2 :

I have also tried this:

Application.Current.Resources.Remove("scaleTransf");
Application.Current.Resources.Add("scaleTransf", new ScaleTransform(val, val));

EDIT3 : My attempt to solve this using a DependencyProperty:

In MainWindow.xaml.cs :

    public static readonly DependencyProperty TransformToApplyProperty = DependencyProperty.Register("TransformToApply", typeof(Transform), typeof(MainWindow));
    public Transform TransformToApply
    {
        get { return (Transform)this.GetValue(TransformToApplyProperty); }
    }

Somewhere in MainWindow, in response to a user input:

this.SetValue(TransformToApplyProperty, new ScaleTransform(val, val));

XAML Style:

<Style TargetType="{x:Type ToolTip}">
    <Setter Property="LayoutTransform" Value="{Binding TransformToApply, Source={x:Reference WndXName}}"/>
...

Using this code, not a single one of the ToolTips seem to scale accordingly.

有帮助吗?

解决方案

I don't think a resource is the best approach in your situation.

It would be better in this case to declare your Transform as a DependencyProperty of your window:

public static readonly DependencyProperty TransformToApplyProperty = DependencyProperty.Register("TransformToApply", typeof(Transform), typeof(Window));

then in XAML:

<Window  .... (all the xmlns)
         x:Name="window"/>
    <AnyControl ScaleTransform="{Binding TransformToApply, ElementName=window}"/>
</Window>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top