Lookless Control的ItemTemplatePanel无法看到控件上的依赖属性

时间:2016-07-04 19:35:49

标签: c# uwp uwp-xaml

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

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

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

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

我的控制班

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>

1 个答案:

答案 0 :(得分:1)

如果我理解正确,那么在模板内的模板中使用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>

然后将其添加到模板中:

while True:

    Input1 = input (" Yum you say after devouring the rabbit unfortunatly someone has overheard you, You have crawled into the old ruins of what looks like big ben, You hear crumbling Someone is in the building with you... Do you escape (Say Escape) or hunt the person down? (Say Hunt)")
    if Input1 in ['Hunt', 'Escape']:
        break

if Input1 == "Hunt":
           print ("its just a friendly trader... or at least thats what you think... you stop worring and run up to him asking him whats going on, He turns around with a grin on his face and you get whacked in the back of your head with a rock... you die on the floor bleeding out!")  

elif Input1 == "Escape":
        print ("you scramble out the abandoned building running for your life and then you see it... a army truck is going down the street... Do you run (say Run) or do you approach them (say Approach")



        Input2 = input (" Yum you say after devouring the rabbit unfortunatly someone has overheard you, You have crawled into the old ruins of what looks like big ben, You hear crumbling Someone is in the building with you... Do you escape (Say Escape) or hunt the person down? (Say Hunt)")
           if Input2 in ['Im', 'Esc']:


            if Input2 == "im":
               print ("its just a friendly trader... or at least thats what you think... you stop worring and run up to him asking him whats going on, He turns around with a grin on his face and you get whacked in the back of your head with a rock... you die on the floor bleeding out!")


           elif Input2 == "Esc":
               print ("you scramble out the abandoned building running for your life and then you see it... a army truck is going down the street... Do you run (say Run) or do you approach them (say Approach")
           else:
               print ("please type Hunt or Escape")        
else:
        print ("please type Hunt or Escape")

这就像一个可继承的属性(在uwp中不存在(但是?)),你可以用模板绑定手动将可视树向下推到内部模板。