工作:靜態資源綁定如果我用這個代碼不作爲的ImageSource
<btn:Button ButtonImage="{Binding Path=BtnImage, FallbackValue=../Images/Search.png}"/>
我的按鈕有一個默認的圖像,如果BtnImage未設置。當我嘗試將其更改爲:
<UserControl.Resources>
<ImageSource x:Key="DefaultImage">
../Images/Search.png
</ImageSource>
</UserControl.Resources>
...
<btn:Button ButtonImage="{Binding Path=BtnImage, FallbackValue={StaticResource DefaultImage}}"/>
我的默認圖像未顯示。我想了解爲什麼以及如何解決這個問題,因爲我是這種StaticResource方法的粉絲。
編輯:
我使用的按鈕是一個虛擬的一個:
<UserControl x:Class="WPF_Controls.Controls.Button"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Name="Btn">
<Button DataContext="{Binding ElementName=Btn}"
Command="{Binding Path=ButtonCommand}">
<Image Source="{Binding Path=ButtonImage}" />
</Button>
</UserControl>
解決方案: 如果我使用:按預期工作
<UserControl.Resources>
<system:String x:Key="DefaultImage">pack://application:,,,/DK_WPF_Controls;component/Images/Search.png</system:String>
</UserControl.Resources>
一切!
也許問題是不是你的fallbackvalue ......你肯定注意到被設置爲實際綁定值'BtnImage'?如果有一些設置你的fallbackvalue將不會使用 –