2012-05-16 28 views
-1

拋出System.Windows.Data錯誤我創建了一個工具欄按鈕內一個風格,是一個圖標和文本的組合:與圖標WPF按鈕樣式只能在運行

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

但在設計TI我一切工作正常...

我該如何解決這個問題?

EDIT(解決方法)

我從按鈕刪除聲明內容屬性:

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

回答

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

我按照你的建議,我得到了「屬性內容已被設置多次」的錯誤。 –

+0

我基於互聯網中找到的示例以此方式設置內容。我不知道其他的方式... –

+0

啊對了,你應該注意到,看編輯的例子。但是,如果它們是兩個始終保持不變的按鈕,我會將它們設置爲模板級別的硬設置值,並根據需要將其作爲靜態資源調用到您的按鈕。 –

0

嗯,你有沒有注意到,它是在抱怨圖像export_excel_16x16.png但您發佈的XAML談論import_16x16.png。你確定你沒有誤解錯誤的原因嗎?即您在其他地方有XAML這是問題。克里斯W.是正確的也檢查圖像的構建行爲。

謝謝

+0

我有兩個按鈕「導入」和「導出」,兩個問題都會發生。儘管我編輯了我的答案。 –