Lookless 控件的 ItemTemplatePanel 无法查看对控件的依赖性 属性

Lookless Control's ItemTemplatePanel Not Able to See Dependency Property on Control

所以当我使用下面的样式时,它会按预期应用于我的控件。但是,GridViewItemsPanelTemplateItemsTemplate)中的模板查看消费者为其数据上下文应用的视图模型。

问题是我想在我的控件中设置项目尺寸。

所以我的问题是,如何将控件模板作为数据上下文应用到 ItemsPanelTemplateItemTemplate

我的第一个想法是使用祖先绑定,但这似乎不是 UWP 中的功能。

我的控制Class

public class FilterableImageWrapGrid : FilterableContentList
    {
        private GridView _partGridView;

        public Point ItemDimensions
        {
            get { return (Point)GetValue(ItemDimensionsProperty); }
            set { SetValue(ItemDimensionsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ItemDimensions.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemDimensionsProperty =
            DependencyProperty.Register("ItemDimensions", typeof(Point), typeof(FilterableImageWrapGrid), new PropertyMetadata(new Point()));

        public FilterableImageWrapGrid()
        {
            DefaultStyleKey = typeof(FilterableImageWrapGrid);
        }

        protected override void OnApplyTemplate()
        {
            _partGridView = GetTemplateChild("PART_FilterableImageList") as GridView;           

            base.OnApplyTemplate();
        }

        private static void OnItemDimensionsChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            FilterableImageWrapGrid wrapGrid = sender as FilterableImageWrapGrid;

            if (wrapGrid != null && wrapGrid._partGridView != null)
            {
                wrapGrid._partGridView.ItemTemplate.SetValue(GridViewItem.WidthProperty, wrapGrid.ItemDimensions.X);
                wrapGrid._partGridView.ItemTemplate.SetValue(GridViewItem.HeightProperty, wrapGrid.ItemDimensions.Y);
            }
        }
    }

我的 Generic.xaml 文件中的风格

 <Style TargetType="controls:FilterableImageWrapGrid">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>                   
                    <Grid VerticalAlignment="Stretch">                        
                        <GridView                                   
                            x:Name="PART_FilterableImageList"
                            ItemsSource="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=FilteredItems, Mode=TwoWay}"
                            SelectedItem="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=SelectedContentItem, Mode=TwoWay}">
                            <GridView.ItemContainerTransitions>
                                <TransitionCollection>
                                    <EntranceThemeTransition  IsStaggeringEnabled="True"/>
                                    <AddDeleteThemeTransition />
                                    <EdgeUIThemeTransition Edge="Left"/>
                                </TransitionCollection>
                            </GridView.ItemContainerTransitions>
                            <GridView.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <ItemsWrapGrid 
                                        x:Name="PART_ItemsWrapGrid"
                                        ItemHeight="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=ItemDimensions.Y}"
                                        ItemWidth="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=ItemDimensions.X}"
                                        Margin="2" Orientation="Horizontal" 
                                        HorizontalAlignment="Center"/>
                                </ItemsPanelTemplate>
                            </GridView.ItemsPanel>
                            <GridView.ItemTemplate>
                                <DataTemplate>
                                   .... Data template that binds to the view model the consumer provides....
                                </DataTemplate>
                            </GridView.ItemTemplate>
                        </GridView>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

如果我理解正确的话,在模板中的模板中绑定 RelativeSource Mode=TemplatedParent 是行不通的。是吗?

然后将 ItemDimensions 属性 定义为附加依赖项 属性:

public class FilterableImageWrapGrid : FilterableContentList

    public static Point GetItemDimensions(DependencyObject obj)
    {
        return (Point)obj.GetValue(ItemDimensionsProperty);
    }

    public static void SetItemDimensions(DependencyObject obj, Point value)
    {
        obj.SetValue(ItemDimensionsProperty, value);
    }

    public static readonly DependencyProperty ItemDimensionsProperty =
        DependencyProperty.RegisterAttached("ItemDimensions", typeof(Point), typeof(ItemsWrapGrid), new PropertyMetadata(new Point()));

    ...
}

然后将其添加到模板中:

                     <GridView                                   
                            x:Name="PART_FilterableImageList"
                            ...
                            <GridView.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <ItemsWrapGrid 
                                        x:Name="PART_ItemsWrapGrid"                                        
                                        ItemHeight="{Binding RelativeSource=
                                 {RelativeSource Mode=Self}, Path=ItemDimensions.Y}"
                                        ItemWidth="{Binding RelativeSource=
                                 {RelativeSource Mode=Self}, Path=ItemDimensions.X}"
                                        Margin="2" Orientation="Horizontal" 
                                        HorizontalAlignment="Center"/>
                                </ItemsPanelTemplate>
                            </GridView.ItemsPanel>
                            ...
                        </GridView>

这就像一个可继承的 属性(在 uwp 中不存在(还没有?)),您手动将可视化树向下推到具有模板绑定的内部模板。