开发者

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 开发者_如何学Pythontooltip for every control. The content of the tooltip should be same as for the helptext extension.

The XAML code:

<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>

And now the code of the style:

<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 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')

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}}}" />


Do you mean binding to attached property? In that case you need additional parentheses:

{Binding Path=(Helper:HelpExtension.HelpText), ...

See, for example, here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜