没有RelativeSource FindAncestor如何绑定?

How to bind without RelativeSource FindAncestor?

我的 Uno UWP 项目需要一些绑定帮助。 我有以下对象:

    public class ViewModel : INotifyPropertyChanged
    {
        public ObservableCollection<Picture> Pictures {get;set;}
        public int ImageWidth {get;set;}
    }
    public class Picture : INotifyPropertyChanged
    {
        public string URL {get;set;}
        public int ImageWidth {get;set;}
    }

我有以下看法:

<Page>
    <Grid>
        <Grid.RowDefinition>
            <RowDefinition Height=*/>
            <RowDefinition Height="Auto"/>
        </Grid.Rowdefinition>   
        <GridView ItemsSource="{Binding Pictures}" SelectionMode="Extended" IsMultiSelectCheckBoxEnabled="False"
                  VerticalAlignment="Stretch" HorizontalAlignment="Stretch" SelectionChanged="ImageSelectionChanged" >
            <GridView.ItemContainerStyle>
                <Style TargetType="GridViewItem">
                    <Setter Property="Margin" Value="10"/>
                </Style>
            </GridView.ItemContainerStyle>
            <GridView.ItemsPanel>
                <ItemsPanelTemplate>
                    <ItemsWrapGrid MaximumRowsOrColumns="5" Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </GridView.ItemsPanel>
            <GridView.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="4">
                        <Image Source="{Binding URL}" Width="{Binding ImageWidth,Mode=TwoWay}" MinWidth="200"/>
                    </Grid>
                </DataTemplate>
            </GridView.ItemTemplate>
        </GridView>
       <Slider Grid.Row="1" Width="200" Minimum="200" Maximum="1500" StepFrequency="100" TickPlacement="Outside" VerticalAlignment="Bottom" Margin="20,0"
                       Value="{Binding ImageWidth, Mode=TwoWay}" />
    </Grid>
</Page> 

页面数据上下文是 ViewModel。这是有效的,因为图片 class 有一个 ImageWidth。相反,我想绑定到 ViewModel.ImageWidth 属性。 我如何在 Uno 中执行此操作?

假设您要Slider改变所有图片的宽度,

您可以像这样更改您的实体来分享 ImageWidth

    public class ViewModel : INotifyPropertyChanged
    {
        public ObservableCollection<Picture> Pictures {get;set;}
        public int ImageWidth {get;set;}
    }
    public class Picture : INotifyPropertyChanged
    {
        public ViewModel Parent {get;}
        public string URL {get;set;}
    }

然后在项目模板中使用以下绑定:

<Image Source="{Binding URL}"
       Width="{Binding Parent.ImageWidth}"
       MinWidth="200"/>