WPF ToolTip样式与动态LayoutTransform [英] WPF ToolTip Style with dynamic LayoutTransform
问题描述
我有一个可以缩放UI的应用程序,我想用它扩展工具提示。
我试过这样做:
< Style TargetType ={x:Type ToolTip}>
< Setter Property =LayoutTransformValue ={DynamicResource scaleTransf}/>
...
< / Style>
...其中 scaleTransf
是一个资源我通过代码更改:
Application.Current.Resources [scaleTransf] = new ScaleTransform(...);
with:
code>< ScaleTransform x:Key =scaleTransfScaleX =1ScaleY =1/>
大多数工具提示符的大小可以缩放,但是其中一些是由C#代码创建的,得到比例。我已经检查,似乎我没有通过代码设置他们的Style或LayoutTransform,所以我真的不明白是什么问题...而且,我有一个印象,上面的XAML代码工作几天前。 :(
我可以做些什么来让所有的时间都没有在代码隐藏中设置LayoutTransform吗?
$ b $
编辑: 如果我将每个 ToolTip
实例的 LayoutTransform
设置为 scaleTransf
在代码背后使用 SetResourceReference()
一切正常,我不明白为什么Style不工作,而它应该做的完全一样每个 ToolTip
被创建...根据我对WPF的有限知识我会称之为一个BUG!
EDIT2:
我也尝试过:
Application.Current.Resources.Remove(scaleTransf);
Application.Current.Resources.Add(scaleTransf,new ScaleTransform(val,val ));
EDIT3:我尝试使用DependencyProperty解决这个问题:
在MainWindow.xaml.cs中:
public static readonly DependencyProperty TransformToApplyProperty = DependencyProperty.Register(TransformToApply,typeof(Transform),typeof(MainWindow));
public Transform TransformToApply
{
get {return(Transform)this.GetValue(TransformToApplyProperty); }
}
在MainWindow的某处,响应用户输入:
this.SetValue(TransformToApplyProperty,new ScaleTransform(val,val));
XAML样式:
< Style TargetType ={x:Type ToolTip}>
< Setter Property =LayoutTransformValue ={Binding TransformToApply,Source = {x:Reference WndXName}}/>
...
使用这段代码,而不是单一的工具提示似乎可以扩展
我不认为资源是您情况下最好的方法。
在这种情况下,将转换声明为窗口的依赖属性会更好:
public静态只读DependencyProperty TransformToApplyProperty = DependencyProperty.Register(TransformToApply,typeof(Transform),typeof(Window));
然后在XAML中:
< Window ....(所有xmlns)
x:Name =window/>
< AnyControl ScaleTransform ={Binding TransformToApply,ElementName = window}/>
< / Window>
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>
这篇关于WPF ToolTip样式与动态LayoutTransform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!