带有图标的WPF按钮样式仅在运行时抛出System.Windows.Data错误

时间:2012-05-16 16:37:53

标签: wpf button styles

我在工具栏中为按钮创建了一个样式,它是图标和文本的组合:

<Style TargetType="{x:Type Button}" x:Key="BtStyle_ToolBar">
    <Setter Property="Foreground" Value="White" />
    <Setter Property="FontWeight" Value="Bold" />
    <Setter Property="FontFamily" Value="Segoe UI" />
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate DataType="{x:Type Button}">
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button}, Path=Tag}" />
                    <TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button}, Path=Content}" 
                               VerticalAlignment="Center" />
                </StackPanel>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

使用它:

<Button Name="Bt_Export" Content="{x:Static p:Resources.Export}" Command="{Binding Path=CmdExport}"
                Padding="5" Style="{StaticResource BtStyle_ToolBar}" Tag="Resources/export_excel_16x16.png"/>

问题是在运行时会向异常窗口抛出异常:

System.Windows.Data Error: 6 : 'ObjectSourceConverter' converter failed to convert value 'Resources/export_excel_16x16.png' (type 'String'); fallback value will be used, if available. BindingExpression:Path=Tag; DataItem='Button' (Name='Bt_Export'); target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource') IOException:'System.IO.IOException: Cannot locate resource 'themes/resources/export_excel_16x16.png'.

但在设计时,一切正常......

我该如何解决这个问题?

编辑(解决方法)

我从按钮声明中删除了content属性:

<Button Name="Bt_Export" Style="{StaticResource BtStyle_ToolBar}">
      <DockPanel>
           <Image Source="/Resources/export_excel_16x16.png"/>
           <TextBlock VerticalAlignment="center" Text="{x:Static p:Resources.Export}"></TextBlock>
       </DockPanel>
 </Button>

2 个答案:

答案 0 :(得分:1)

<Button Name="Bt_Import" Command="{Binding Path=CmdImport}" Style="{StaticResource BtStyle_ToolBar}">
  <StackPanel>
    <TextBlock Text="{x:Static p:Resources.Import}"/>
    <Image Source="Resources/import_16x16.png"/>
  </StackPanel>
</Button>

或者只是在模板级别设置它。

<Style TargetType="{x:Type Button}" x:Key="BtStyle_ToolBar">
    <Setter Property="Foreground" Value="White" />
    <Setter Property="FontWeight" Value="Bold" />
    <Setter Property="FontFamily" Value="Segoe UI" />
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate DataType="{x:Type Button}">
                <StackPanel Orientation="Horizontal">
                    <Image Source="Resources/import_16x16.png" />
                    <TextBlock Text="Awesome" 
                               VerticalAlignment="Center" />
                </StackPanel>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

答案 1 :(得分:0)

嗯,你注意到它所抱怨的图片是export_excel_16x16.png,但你发布的XAML正在谈论import_16x16.png。你确定你没有误解错误的原因吗?即你在其他地方有XAML就是问题。而Chris W.也是正确的,还要检查图像的构建动作。

由于