简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM