我有一个带有ResourceKey和标题的列表值,这两个值都是字符串。资源是资源字典中定义的实际资源的名称。每个ResourceKey图标都是画布图标。
<Data ResourceKey="IconCalendar" Caption="Calendar"/>
<Data ResourceKey="IconEmail" Caption="Email"/>然后我有一个列表视图,它有一个带有按钮的数据模板和一个按钮下面的文本标题。我想做的是将资源静态资源显示为按钮的内容。
<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>我想我已经尝试过绑定、静态、资源等所有的排列了。
我对其他选择持开放态度,我知道只有一个图像并设置source属性可能会更容易。
谢谢
发布于 2009-03-29 23:58:23
经过一些思考后,我最终使用了这样的ValueConvertor:
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}}" />https://stackoverflow.com/questions/695624
复制相似问题