绑定到附加属性 [英] Bind to a attached property
问题描述
我写了一个附加属性,其中存储了帮助文本。
此帮助文本显示在主窗口的右侧。
I have written a attached property which stores amongst others a help text. This help text is shown on the right side of the main window. This works fine.
现在,我想为每个控件添加一个工具提示。工具提示的内容应与帮助文本扩展名的内容相同。
Now, I want to add a tooltip for every control. The content of the tooltip should be same as for the helptext extension.
XAML代码:
<ListView ctrl:ListViewLayoutManager.Enabled="true"
x:Name="ListViewSources"
ItemsSource="{Binding SourceItems}"
ItemContainerStyle="{DynamicResource ListViewItemStyleAlternate}"
Height="150"
MinWidth="350"
Helper:HelpExtension.IsControl="true"
Helper:HelpExtension.HelpText="{x:Static strings:GUIResource.HelpProfilesSourcesDescriptionText}" >
<ListView.ToolTip>
<ToolTip Style="{DynamicResource Own_TooltipStyle}"></ToolTip>
</ListView.ToolTip>
现在是样式代码:
<Style x:Key="Own_TooltipStyle" TargetType="{x:Type ToolTip}">
<Setter Property="Background" Value="LightYellow"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToolTip}">
<Grid>
<TextBlock Text="{Binding Path=Helper:HelpExtension.HelpText, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
如果我运行程序,则会出现绑定错误:
If I run the program I get the binding error:
System.Windows.Data错误:39:
BindingExpression路径错误:
'Helper:HelpExtension'属性未在'object''上找到
'ListView'
(Name ='ListViewSources')'。
BindingExpression:Path = Helper:HelpExtension.HelpText;
DataItem ='ListView'
(名称='ListViewSources');目标
元素是 TextBlock(名称=);
的目标属性是文本(类型
'字符串')
System.Windows.Data Error: 39 : BindingExpression path error: 'Helper:HelpExtension' property not found on 'object' ''ListView' (Name='ListViewSources')'. BindingExpression:Path=Helper:HelpExtension.HelpText; DataItem='ListView' (Name='ListViewSources'); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
有人知道我可以绑定吗将TextBlock转换为Helper:HelpExternsion.HelpText的内容?
Does anybody know how I can bind the TextBlock to the content of Helper:HelpExternsion.HelpText?
推荐答案
只需在附加的属性名称周围加上括号即可:
Just put parentheses around the attached property name :
<TextBlock Text="{Binding Path=(Helper:HelpExtension.HelpText), RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}}" />
这篇关于绑定到附加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!