2016-12-06 93 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