2016-12-06 92 views
0

我想覆蓋我的BoardSquares上的圖像,但是當試圖在我的Image上指定Source時,它說The member "Source" is not recognized or is not accessible。任何想法我可能做錯了什麼? P.S我省略了DataTemplate觸發器,但他們在那裏。WPF圖片來源無法識別或無法訪問

<ItemsControl ItemsSource="{Binding BoardGUI.BoardSquares}"> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <UniformGrid Rows="10" Columns="10"/> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 

    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Button x:Name="Square" 
        Command="{Binding DataContext.BoardGUI.SquareClickCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}" 
        CommandParameter="{Binding}"> 
       <Button.Template> 
        <ControlTemplate TargetType="Button"> 
         <Grid Background="{TemplateBinding Background}"> 
          <Image Source="{TemplateBinding Source}"/> 
         </Grid> 
        </ControlTemplate> 
       </Button.Template> 
      </Button> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 
+0

Button類沒有Source屬性,'{TemplateBinding來源}'將無法工作。這是什麼'源'? – ASh

回答

1

任何想法,我可能做錯了什麼?

{TemplateBinding Source}嘗試綁定到模板父級的Source屬性,在這種情況下它是Button,並且Button沒有Source屬性。

如果BoardSquares源集合中的對象的類型有一個Source屬性,你應該使用{結合}標記擴展綁定到這一點:

<Button.Template> 
    <ControlTemplate TargetType="Button"> 
    <Grid Background="{TemplateBinding Background}"> 
     <Image Source="{Binding Source}"/> 
    </Grid> 
    </ControlTemplate> 
</Button.Template> 

如果你只是想設置Source屬性圖像以非動態圖像源的,你可以只指定一個URI到該圖像:

<Image Source="Images/pic.png"/> 
+0

我更喜歡解答問題並提供答案的答案+1 – MikeT