2016-03-05 46 views
2

當我嘗試綁定圖像源時遇到了一些問題。我正在創建一個棋盤遊戲,您可以選擇玩家查看其屬性(包括他的圖像)。此外,電路板上的字段具有圖像作爲背景。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> 

它的工作原理。

+0

(旁註)你確定你不想要//應用程序:並將圖像放在與你的exe相同的目錄中?仍然在想你的原始問題......你有目標類型的按鈕......按鈕在哪裏? –

+0

爲什麼會更好? 這個模板是在另一個xaml文件中,而不是按鈕本身,我不認爲這是問題的必要條件,因爲如果我用一個外部源(如c:\ something \ testField.jpg)替換圖像源,它可以很好地工作。 –

+0

RotateTransform工作,但圖像不? –

回答

1

讓你的財產的ImageSource而不是字符串,或使用一個轉換器就像這個答覆中提到:Binding image source through property in wpf。當你對字符串進行硬編碼時,xaml編譯器正在爲你做這些工作。

+0

謝謝!雖然這樣做,我發現我做了noob錯誤,所以我接受你的答案。 –

1

我可以看到你嘗試將ImageBrush.ImageSource綁定到資源。

我找到了關於Cannot set ImageSource using pack URI的stackoverflow答案,我認爲這是你的情況。在回答未來

關注:

...請注意,如果您將圖像添加到Resource.resx時,Visual Studio生成與每個圖像靜態特性的資源類。 這些屬性類型System.Drawing.Bitmap,這是的WinForms, 不是WPF

更新的:你也可以使用轉換器更準確地獲取資源。

public class StringToImageSource: IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, string language) 
     { 
      string resourceName = value.ToString(); 

      var img = new BitmapImage(); 
      img.UriSource = new Uri("Resources/" + resourceName); 

      return img; 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, string language) 
     { 
      throw new NotImplementedException(); 
     } 
} 
+0

不幸的是,這不是它。我也瀏覽了這個問題中的鏈接,他們都沒有解決我的問題 –

+0

可能你可以嘗試直接在xaml中使用你的URI 並在理由之後作出一些結論。 – Anton

+0

這個問題是我想爲每個字段設置不同的圖片 –

相關問題