當我嘗試綁定圖像源時遇到了一些問題。我正在創建一個棋盤遊戲,您可以選擇玩家查看其屬性(包括他的圖像)。此外,電路板上的字段具有圖像作爲背景。WPF圖像源綁定有時候有時候不會工作
我的字段創建模板:
<ControlTemplate x:Key="fieldButtonTemplate" x:Name="fieldButtonTemplateName" TargetType="{x:Type Button}">
<Grid x:Name="fieldGrid" Visibility="Visible">
<Grid.LayoutTransform>
<RotateTransform Angle="{Binding ImageRotate}" />
</Grid.LayoutTransform>
<Grid.Background>
<ImageBrush ImageSource="{Binding Path=ImageSource}"/>
</Grid.Background>
<Grid.RowDefinitions>
....
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
...
</Grid.ColumnDefinitions>
</Grid>
</ControlTemplate>
而一個用於查看球員屬性:
<Image Source="{Binding Path=CurrentlySelected.ImageSource}" Grid.Column="0" Grid.Row="0"
Grid.ColumnSpan="2" Stretch="Fill" />
<StackPanel x:Name="CurrentPlayerStackPanel" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2">
<TextBlock Text="{Binding Path=CurrentlySelected.Name}" TextWrapping="Wrap"
FontFamily="Magneto" FontSize="20" Foreground="BlanchedAlmond"/>
<TextBlock Text="{Binding Path=CurrentlySelected.Character}" TextWrapping="Wrap"
FontFamily="Magneto" FontSize="20" Foreground="BlanchedAlmond"/>
<TextBlock Text="{Binding Path=CurrentlySelected.Money}" TextWrapping="Wrap"
FontFamily="Magneto" FontSize="20" Foreground="BlanchedAlmond"/>
</StackPanel>
的問題是圖像只顯示在選擇球員,並且不顯示爲電網的背景。 我創建了雙方球員和領域的接口:
interface IVisualObject
{
int ImageRotate { get; set; }
string ImageSource { get; set; }
}
的圖像資源,構建操作設置爲內容並複製到輸出目錄設置是否有更新的複製。我試圖通過爲圖像源的字符串是像
「包:// siteoforigin:,,, /資源/ picture.jpg」
BTW ImageRotate被綁定就好了。 在此先感謝!
編輯: 如果我把它像
<Grid.Background>
<ImageBrush ImageSource="pack://siteoforigin:,,,/Resources/picture.jpg"/>
</Grid.Background>
它的工作原理。
(旁註)你確定你不想要//應用程序:並將圖像放在與你的exe相同的目錄中?仍然在想你的原始問題......你有目標類型的按鈕......按鈕在哪裏? –
爲什麼會更好? 這個模板是在另一個xaml文件中,而不是按鈕本身,我不認爲這是問題的必要條件,因爲如果我用一個外部源(如c:\ something \ testField.jpg)替換圖像源,它可以很好地工作。 –
RotateTransform工作,但圖像不? –