How to have a property of a view model set the height/width for all associate child controls
Here's the scenario: I have a flickr photo viewer, and the PhotoBrowserViewModel has a property named Image Source. The View that is bound to my PhotoBrowserViewModel contains the following XAML. My goal is to get the child controls to have the height/width of the ImageSource property.
<ItemsControl ItemsSource="{Binding Photos}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="viewmodel:FlickrPhotoViewModel">
<con开发者_高级运维trols:FlickrPhotoControl Margin="10"/>
<!-- I want to set this control's Width/Height to {Binding PhotoSize}-->
<!-- on the same data context as ItemsControl, not as the data template.-->
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Use relative source binding:
{Binding RelativeSource={RelativeSource AncestorType=ItemsControl}, Path=DataContext.PhotoSize}
精彩评论