绑定一个DataContext字符串属性为静态资源关键 [英] Binding a datacontext string property to a StaticResource key
问题描述
我有一个的ResourceKey和标题的列表值,这些值都是字符串。资源是在一个资源字典中定义的实际资源的名称。每个图标的ResourceKey是帆布的。
I have an List values with a ResourceKey and a Caption, these values are both strings. The Resource is the name of an actual resource defined in a resource dictionary. Each of these ResourceKey Icons are Canvas's.
<Data ResourceKey="IconCalendar" Caption="Calendar"/>
<Data ResourceKey="IconEmail" Caption="Email"/>
然后我有它有一个按钮,按钮下方的文本标题一个DataTemplate的列表视图。我想要做的是显示资源的静态资源作为按钮的内容。
I then have a list view which has a datatemplate with a button and a text caption below the button. What I want to do is display Resource static resource as the content for the button.
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Button Content="{Binding ResourceKey}" Template="{StaticResource RoundButtonControlTemplate}"/>
<TextBlock Grid.Row="1" Margin="0,10,0,0" Text="{Binding Caption}" HorizontalAlignment="Center" FontSize="20" FontWeight="Bold" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
我想我已经想尽排列与结合StaticResource的等。
I think I have tried every permutation with binding staticresource etc.
我打开替代品,我知道这可能会更易于只是有一个形象,并设置源属性。
I am open to alternatives, I know it may be easier to just have an image and set the source property.
感谢
推荐答案
有一个小以为我结束了使用后 ValueConvertor
像这样:
After having a little think I ending up using a ValueConvertor
like so:
class StaticResourceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var resourceKey = (string)value;
return Application.Current.Resources[resourceKey];
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new Exception("The method or operation is not implemented.");
}
}
和按钮上的约束力变为
<Button Content="{Binding ResourceKey, Converter={StaticResource resourceConverter}}" />
这篇关于绑定一个DataContext字符串属性为静态资源关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!