C# 如何让视图模型的属性设置所有关联子控件的高度/宽度

C# 如何让视图模型的属性设置所有关联子控件的高度/宽度,c#,.net,wpf,mvvm,binding,C#,.net,Wpf,Mvvm,Binding,下面是一个场景:我有一个flickr照片查看器,PhotoBrowserViewModel有一个名为Image Source的属性。绑定到我的PhotoBrowserViewModel的视图包含以下XAML。 我的目标是使子控件具有ImageSource属性的高度/宽度 <ItemsControl ItemsSource="{Binding Photos}"> <ItemsControl.ItemsPanel>

下面是一个场景:我有一个flickr照片查看器,PhotoBrowserViewModel有一个名为Image Source的属性。绑定到我的PhotoBrowserViewModel的视图包含以下XAML。 我的目标是使子控件具有ImageSource属性的高度/宽度

        <ItemsControl ItemsSource="{Binding Photos}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate DataType="viewmodel:FlickrPhotoViewModel">
                    <controls: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>

使用相对源绑定:

{Binding RelativeSource={RelativeSource AncestorType=ItemsControl}, Path=DataContext.PhotoSize}

使用相对源绑定:

{Binding RelativeSource={RelativeSource AncestorType=ItemsControl}, Path=DataContext.PhotoSize}

由于某些原因,相对的源代码绑定总是让我感觉脏兮兮的。哈哈,我不在乎,去做吧!(或者,你可以在你的资源中创建一个引用,这样你就可以使用
Source={StaticResource MyContext}
。你也可以用ElementName做一些事情,例如,命名你的窗口并获取它的DataContext)相对的源绑定总是让我因为某种原因而感到肮脏。哈哈,我不在乎,做吧!(或者,您可以在资源中创建引用,以便使用
Source={StaticResource MyContext}
。您还可以使用ElementName执行某些操作,例如命名窗口并获取其数据上下文)