访问staticResource中的属性

时间:2017-01-13 09:08:01

标签: c# wpf xaml

我正在尝试使用xaml中定义的资源的属性,如下所示:

<Window.Resources>
    <map:TileLayer x:Key="OpenStreetMap" SourceName="OpenStreetMap"
                   Description="Maps © [OpenStreetMap Contributors](http://www.openstreetmap.org/copyright)"
                   TileSource="http://{c}.tile.openstreetmap.org/{z}/{x}/{y}.png"
                   MaxZoomLevel="14"/>

然后使用 Description 属性设置TextBlock的 Text 。 我试过这个,但它不起作用。

<TextBlock Text="{Binding Source={StaticResource OpenStreetMap.Description}}"/>
<TextBlock Text="{StaticResource OpenStreetMap.Description}"/>

如何在xaml中定义的资源中访问该属性?

1 个答案:

答案 0 :(得分:5)

这样:

<TextBlock Text="{Binding Source={StaticResource OpenStreetMap}, Path=Description}"/>

或更短:

<TextBlock Text="{Binding Description, Source={StaticResource OpenStreetMap}}"/>

请注意,Description属性包含markdown文本。你可以像这样使用HyperlinkText助手类:

<TextBlock map:HyperlinkText.InlinesSource="{Binding Description,
                                             Source={StaticResource OpenStreetMap}}"/>
相关问题