Dockson
Dockson

Reputation: 562

WPF Image Source is not recognized or is not accessible

I am trying to overlay an image on my BoardSquares but when trying to specify a Source on my Image it says The member "Source" is not recognized or is not accessible. Any idea what I could be doing wrong? P.S I omitted the DataTemplate Triggers but they're there.

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

Upvotes: 0

Views: 916

Answers (1)

mm8
mm8

Reputation: 169150

Any idea what I could be doing wrong?

{TemplateBinding Source} tries to bind to a Source property of the templated parent which is the Button in this case and a Button has no Source property.

If the type of objects in the BoardSquares source collection has a Source property you should use the {Binding} markup extension to bind to this:

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

If you simply want to set the Source property of the Image to a non-dynamic image source, you could just specify a Uri to this image:

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

Upvotes: 1

Related Questions