gpt4 book ai didi

wpf - 将数据上下文字符串属性绑定(bind)到 StaticResource 键

转载 作者:行者123 更新时间:2023-12-03 03:47:47 24 4
gpt4 key购买 nike

我有一个带有 ResourceKey 和 Caption 的列表值,这些值都是字符串。 Resource是资源字典中定义的实际资源的名称。这些 ResourceKey 图标中的每一个都是 Canvas 的。

<Data ResourceKey="IconCalendar" Caption="Calendar"/>
<Data ResourceKey="IconEmail" Caption="Email"/>

然后我有一个 ListView ,其中有一个带有按钮的数据模板和按钮下方的文本标题。我想要做的是将 Resource 静态资源显示为按钮的内容。

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

我想我已经尝试了绑定(bind)静态资源等的所有排列。

我对替代方案持开放态度,我知道仅拥有图像并设置源属性可能会更容易。

谢谢

最佳答案

经过一番思考后,我最终使用了一个 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.");
}
}

按钮上的绑定(bind)变为

<Button Content="{Binding ResourceKey, Converter={StaticResource resourceConverter}}" />

关于wpf - 将数据上下文字符串属性绑定(bind)到 StaticResource 键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/695624/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com