拋出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>
我按照你的建議,我得到了「屬性內容已被設置多次」的錯誤。 –
我基於互聯網中找到的示例以此方式設置內容。我不知道其他的方式... –
啊對了,你應該注意到,看編輯的例子。但是,如果它們是兩個始終保持不變的按鈕,我會將它們設置爲模板級別的硬設置值,並根據需要將其作爲靜態資源調用到您的按鈕。 –