问题描述
我的目标是将 Label 的 Content-Property 绑定到 Style 元素的 Tag-Property应用于.但我的解决方案似乎不起作用:
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:
我的风格:
<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>
我的文本框:
<TextBox
Style="{StaticResource ResourceKey=HintedTextBox}"
x:Name="tbTest" />
推荐答案
如果我没理解错,您想设置 VisualBrush
的文本,该文本将显示在 TextBox代码>.
If I understand correctly, you want to set the text for VisualBrush
, that will be displayed in the TextBox
.
你可以这样做:
<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.
正如你可能理解的,看我的例子,RelativeSource
必须不是 self,在在这种情况下,它将指向自身 (VisualBrush
),并且具有该类型的元素必须是 TextBox
,位于可视化树中的较高位置.
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.
与 RelativeSource
绑定在资源中不起作用,因为 Resource
不是资源的一部分可视化树,或模板的一部分.
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.
在样式中,这种构造不起作用,因为 Style
只是 setter 的集合,他不知道控件, 在那里.为此,通常使用 DataTemplate
或 ControlTemplate
.
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
.
作为替代方案,在这种情况下,我建议为 TextBox
使用模板,该模板将注册为 VisualBrush
.
As an alternative, in this case, I suggest using a template for the TextBox
, which will be registered VisualBrush
.
下面是我的例子:
<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>
输出
这篇关于WPF - 样式中的相对源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!