Reputation: 556
I'm developing an application using C# and XAML and I've encountered a problem that is confusing me. I have a property in my data called GroupImage
and have used binding to set the Source
property of an Image
with it. That worked fine but when I wanted to do the same thing a second time it doesn't show the image in the second Image
control.
<Image Source="{Binding Group.GroupImage}" Width="250" Height="500" Stretch="UniformToFill" />
<Image VerticalAlignment="Bottom" Stretch="UniformToFill" Source="{Binding Group.GroupImage}" Grid.RowSpan="2"/>
The top one works fine the bottom one doesn't. I have been reading about Data Binding and have gotten the impression that you need to specify something in the DataContext
to use a property more than once. Is this right? It seems a very strange way of doing this.
I am relatively new to C# so sorry if I'm missing something obvious. I'd appreciate a more knowledgeable cluing me in.
Thanks
Update Following the assistance I received I figured out that the context was being set to
DataContext="{Binding Group}"
And as a result my second line needed to change to the following since the Data Context was already set to Group.
<Image VerticalAlignment="Bottom" Stretch="UniformToFill" Source="{Binding GroupImage}" Grid.RowSpan="2"/>
Upvotes: 1
Views: 689
Reputation: 2996
You don't need to specify something in the DataContext
to use a property more than once. But your two Image
have to have the right DataContext
(you can easily test it with the debugger), depending on their location on the visual tree (You didn't provide any code for the DataContext
part ?) .
You can also check that your Image
's Width
/Height
are not 0.
Upvotes: 1