【问题标题】:StaticResource cannot find IValueConverterStaticResource 找不到 IValueConverter
【发布时间】:2015-06-17 11:03:59
【问题描述】:

所以我尝试在 XAML 中设置一些按钮的样式,但遇到了绑定静态资源的问题。

我试图让按钮拥有一个 Grid 系统,有 2 个 cols。左边是图片,右边是 contentpresenter(或文本)。我希望此按钮的每个实例的图像都不同,因此图像上的 Source 需要不同。我可能以错误的方式处理这个问题,但我能想到的让按钮 XAML 反映控件模板内的不同图像的唯一方法是使用按钮的 Tag 属性,将其设置为源字符串。然后使用 IValueConverter 将字符串转换为位图图像。但是绑定给了我一个可怕的时间。我已经搜索了所有我能找到的东西,但我一生都无法弄清楚!

这里是样式代码:

  <Style x:Key="ServerButtonTop" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="#0d5b8f" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="Width" Value="220"/>
        <Setter Property="Height" Value="60" />
        <Setter Property="VerticalAlignment" Value="Top" />
        <Setter Property="Margin" Value="20" />
        <Setter Property="FontSize" Value="20" />
        <!-- Template for the buttons -->
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border BorderBrush="#dddddd" BorderThickness="1" Background="{TemplateBinding Background}" CornerRadius="3">
                        <Grid Width="{TemplateBinding Width}">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="50" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Image Width="40" Height="40" Source="{TemplateBinding Tag, Converter={StaticResource StringToImage}}" />
                            <ContentPresenter Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Center" Name="thetext" />
                        </Grid>
                    </Border>
                    <!-- trigger for mouse over -->
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Foreground" Value="Black" />
                            <Setter Property="Background" Value="#ACD9F8" />
                         </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="RenderTransform" TargetName="thetext">
                                <Setter.Value>
                                    <TranslateTransform Y="1.0" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

问题在于行:

<Image Width="40" Height="40" Source="{TemplateBinding Tag, Converter={StaticResource StringToImage}}" />

为了完成 XAML 部分,这就是我想要实现的目标:

<Button Style="{StaticResource ServerButtonTop}" Content="Button Text" Tag="Resources/imagename.png" />

整个按钮完美运行,样式和网格布局等一切,只是它不会显示图像。

这是 IValueConverter 类的代码

    [ValueConversion(typeof(string), typeof(BitmapImage))]
public class StringToImage : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo info)
    {

        return new BitmapImage(new Uri((string)value));

    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo info)
    {
        throw new NotImplementedException();
    }
} 

目前我遇到的问题是“无法解决资源‘StringToImage’。”在玩的时候,我得到了“IValueConvert 无法从字符串转换”的错误。

IValueConverter 类就在同一个 XAML 窗口的命名空间中的窗口类旁边。

【问题讨论】:

  • 您是否真的在窗口资源中创建了转换器的实例并为其分配了键StringToImage
  • @Clemens 感谢您指出这一点。我确实把它放在那里,但我把它放在模板的资源而不是窗口的资源下。愚蠢的错误。感谢您找到它!

标签: .net wpf xaml


【解决方案1】:

在没有大量位图的情况下进行测试有点棘手。我认为这应该可行:

<Image Width="40" Height="40" Source="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource StringToImage}}" />

很好地解释了为什么这应该在这里工作:WPF TemplateBinding vs RelativeSource TemplatedParent

【讨论】:

    猜你喜欢
    • 2017-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多