WPF图像源无法识别或无法访问

时间:2016-12-06 11:11:22

标签: c# wpf xaml

我正在尝试在我的BoardSquares上叠加图片,但在尝试在Source上指定Image时,它会显示The member "Source" is not recognized or is not accessible。知道我可能做错了吗? P.S我省略了DataTemplate触发器,但它们就在那里。

<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>

1 个答案:

答案 0 :(得分:1)

  

知道我可能做错了吗?

{TemplateBinding Source}尝试绑定到模板化父级的Source属性,在这种情况下是Button,而Button没有Source属性。

如果BoardSquares源集合中的对象类型具有Source属性,则应使用 {Binding} 标记扩展名绑定到此属性:

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

如果您只想将图像的Source属性设置为非动态图像源,则只需为此图像指定一个Uri:

<Image Source="Images/pic.png"/>
相关问题