WPF - 中的RelativeSource风格 [英] WPF - RelativeSource in Style
问题描述
我的目标是绑定的内容的的标签的的标签的元素的风格,物业是,物业适用于的。但我的解决方案似乎没有工作:
我的风格:
<风格
的TargetType =文本框
X:键=HintedTextBox>
< Style.Resources>
<的VisualBrush
AlignmentX =左
AlignmentY =中心
拉伸=无
X:键=HintedTextBox_Hint>
< VisualBrush.Visual>
<标签
CONTENT ={绑定的RelativeSource = {自我的RelativeSource},路径=标签}
前景=浅灰色/>
< /VisualBrush.Visual>
< /&的VisualBrush GT;
< /Style.Resources>
<! - 触发器 - >
< /样式和GT;
我的文本框:
<文本框
风格={StaticResource的的ResourceKey = HintedTextBox}
X:NAME =tbTest/>
如果我理解正确的话,你要设置为的VisualBrush
的文字,将显示在文本框
。
您可以做到这一点是这样的:
<文本框名称=MyTextBox标签=MyNewValueWIDTH =100HEIGHT =25>
< TextBox.Background>
<的VisualBrush AlignmentX =左AlignmentY =中心拉伸=无>
< VisualBrush.Visual>
<标签内容={绑定的RelativeSource = {的RelativeSource AncestorType =文本框},路径=标签}前景=浅灰色/>
< /VisualBrush.Visual>
< /&的VisualBrush GT;
< /TextBox.Background>
< /文本框>
为了解释为什么不赚你的例子:
1
正如你可能知道,看着我的例子,的RelativeSource
必须是的不的自我,在这种情况下,它会指向其自身(的VisualBrush
),并与该类型的元素必须是文本框
,坐落在可视化树高。
2
与的RelativeSource
绑定资源不起作用,因为资源
不是可视化树的一部分,或者模板的一部分。
3
在风格这种结构将无法工作,因为风格
是刚刚制定者的集合,他不知道控制,在那里。为此,通常使用的DataTemplate
或控件模板
。
作为替代,在这种情况下,我建议使用的文本框
,将要被登记的模板的VisualBrush
。
下面是我的例子:
< Window.Resources>
<风格的TargetType ={X:TextBox类型}>
< setter属性=SnapsToDevicePixelsVALUE =真/>
< setter属性=OverridesDefaultStyleVALUE =真/>
< setter属性=KeyboardNavigation.TabNavigationVALUE =无/>
< setter属性=FocusVisualStyleVALUE ={X:空}/>
< setter属性=了minWidthVALUE =120/>
< setter属性=了minHeightVALUE =20/>
< setter属性=的AllowDropVALUE =真/> < setter属性=模板>
< Setter.Value>
<的ControlTemplate的TargetType ={X:类型TextBoxBase}>
< BORDER NAME =边框CornerRadius =0填充=2了borderThickness =1BorderBrush =黑>
< Border.Background>
<的VisualBrush AlignmentX =左AlignmentY =中心拉伸=无>
< VisualBrush.Visual>
<标签内容={绑定的RelativeSource = {的RelativeSource TemplatedParent},路径=标签}
前景=浅灰色/>
< /VisualBrush.Visual>
< /&的VisualBrush GT;
< /Border.Background> <的ScrollViewer保证金=0X:NAME =PART_ContentHost/>
< /边框>
< /控件模板>
< /Setter.Value>
< /二传手>
< /样式和GT;
< /Window.Resources><网格和GT;
<文本框名称=MyTextBox标签=MyNewValueWIDTH =100HEIGHT =25/>
< /网格和GT;
输出
My target is to bind the Content-Property of the Label to the Tag-Property of the Elements the Style is applied to. But my solution doesn't seem to work:
My style:
<Style
TargetType="TextBox"
x:Key="HintedTextBox">
<Style.Resources>
<VisualBrush
AlignmentX="Left"
AlignmentY="Center"
Stretch="None"
x:Key="HintedTextBox_Hint">
<VisualBrush.Visual>
<Label
Content="{Binding RelativeSource={RelativeSource Self}, Path=Tag}"
Foreground="LightGray" />
</VisualBrush.Visual>
</VisualBrush>
</Style.Resources>
<!-- Triggers -->
</Style>
My textbox:
<TextBox
Style="{StaticResource ResourceKey=HintedTextBox}"
x:Name="tbTest" />
If I understand correctly, you want to set the text for VisualBrush
, that will be displayed in the TextBox
.
You can do it like this:
<TextBox Name="MyTextBox" Tag="MyNewValue" Width="100" Height="25">
<TextBox.Background>
<VisualBrush AlignmentX="Left" AlignmentY="Center" Stretch="None">
<VisualBrush.Visual>
<Label Content="{Binding RelativeSource={RelativeSource AncestorType=TextBox}, Path=Tag}" Foreground="LightGray" />
</VisualBrush.Visual>
</VisualBrush>
</TextBox.Background>
</TextBox>
In order to explain why your example not earned:
1.
As you probably understand, looking at my example, RelativeSource
must be not self, in which case it will point to itself (VisualBrush
), and the element with the type must be of TextBox
, located higher in the visual tree.
2.
Binding with RelativeSource
does not work in resources, because the Resource
is not part of the visual tree, or part of the template.
3.
In styles this construction will not work, because the Style
is just the collection of setters, he does not know about control, are there. For this purpose, usually using DataTemplate
or ControlTemplate
.
As an alternative, in this case, I suggest using a template for the TextBox
, which will be registered VisualBrush
.
Below is my example:
<Window.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="KeyboardNavigation.TabNavigation" Value="None" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="MinWidth" Value="120" />
<Setter Property="MinHeight" Value="20" />
<Setter Property="AllowDrop" Value="true" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBoxBase}">
<Border Name="Border" CornerRadius="0" Padding="2" BorderThickness="1" BorderBrush="Black">
<Border.Background>
<VisualBrush AlignmentX="Left" AlignmentY="Center" Stretch="None">
<VisualBrush.Visual>
<Label Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag}"
Foreground="LightGray" />
</VisualBrush.Visual>
</VisualBrush>
</Border.Background>
<ScrollViewer Margin="0" x:Name="PART_ContentHost" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<TextBox Name="MyTextBox" Tag="MyNewValue" Width="100" Height="25" />
</Grid>
Output
这篇关于WPF - 中的RelativeSource风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!