简体   繁体   English

UWP GridViewItem 将厚度绑定到 ItemIndex

[英]UWP GridViewItem Binding Thicknesss to ItemIndex

I have an AdaptiveGridView control with an ItemTemplate that consists of a Grid containing a StackPanel with an Icon and a TextBlock.我有一个带有 ItemTemplate 的 AdaptiveGridView 控件,该控件由一个网格组成,该网格包含一个带有图标的 StackPanel 和一个 TextBlock。 I want to give each Grid of the GridViewItem's ItemTemplate a different Thickness.我想给 GridViewItem 的 ItemTemplate 的每个 Grid 一个不同的厚度。 Researching on how to do so, I found that binding the Thickness parameter to a IValueConverter is the best way to do it.研究如何做到这一点,我发现将厚度参数绑定到 IValueConverter 是最好的方法。 The issue is that when I try it, I get an error stating:问题是,当我尝试它时,我收到一条错误消息:

System.InvalidCastException: 'Unable to cast object of type 'Test.Models.SampleOrder' to type 'Windows.UI.Xaml.Controls.GridViewItem'.' System.InvalidCastException:“无法将“Test.Models.SampleOrder”类型的 object 转换为“Windows.UI.Xaml.Controls.GridViewItem”。

This is what I'm trying:这就是我正在尝试的:

Page.xaml页.xaml

<Page.Resources>
    <converter:IndexToBorderThicknessConverter x:Name="indexconverter"/>
</Page.Resources>

<Grid x:Name="ContentArea" VerticalAlignment="Center">
    <controls:AdaptiveGridView
        ItemsSource="{x:Bind ViewModel.Source,Mode=OneWay}"
        <controls:AdaptiveGridView.ItemTemplate>
            <DataTemplate x:DataType="models:SampleOrder">
                <Grid
                    x:Name="itemThumbnail"  
                    BorderBrush="Gray"
                    BorderThickness="{Binding Converter={StaticResource indexconverter}}"
                    Padding="{StaticResource XSmallLeftTopRightBottomMargin}"
                    Background="White">
                    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                        <FontIcon Glyph="{x:Bind Symbol}" />
                        <TextBlock
                            Margin="{StaticResource XXSmallTopMargin}"
                            HorizontalAlignment="Center"
                            Style="{ThemeResource BodyTextStyle}"
                            Text="{x:Bind Company}" />
                    </StackPanel>
                </Grid>
            </DataTemplate>
        </controls:AdaptiveGridView.ItemTemplate>
    </controls:AdaptiveGridView>
</Grid>

IndexToBorderThicknessConverter.cs IndexToBorderThicknessConverter.cs

public class IndexToBorderThicknessConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        GridViewItem item = (GridViewItem)value;
        AdaptiveGridView gridView = ItemsControl.ItemsControlFromItemContainer(item) as AdaptiveGridView;
        int index = gridView.Items.IndexOf(item);
        Thickness thickness;
        if (index % 2 == 0)
        {
            thickness = new Thickness(1);
        }

        thickness = new Thickness(2);

        return thickness;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

I can see that what it's being sent to the converter is the model of the SampleOrder and not the GridViewItem.我可以看到它被发送到转换器的是 SampleOrder 的 model 而不是 GridViewItem。 So my question is: How to send the GridViewItem insted of the SampleOrder or how to get the GridViewItem from the SampleOrderSent?所以我的问题是:如何发送 SampleOrder 的 GridViewItem 或如何从 SampleOrderSent 获取 GridViewItem?

Thanks a lot in advanced.非常感谢先进。

Cheers!!!干杯!!!

Instead of using a converter, you could handle the Loaded event for the Grid element in the template and use the VisualTreeHelper class to find the parent GridViewItem` in the visual tree:您可以处理模板for the Grid element in the template and use theLoaded事件,而不是使用转换器,并使用 VisualTreeHelper class to find the parent GridViewItem`:

private void itemThumbnail_Loaded(object sender, RoutedEventArgs e)
{
    Grid grid = (Grid)sender;
    GridViewItem item = FindParent<GridViewItem>(grid);
    AdaptiveGridView gridView = ItemsControl.ItemsControlFromItemContainer(item) as AdaptiveGridView;
    int index = gridView.Items.IndexOf(grid.DataContext);
    grid.BorderThickness = (index % 2 == 0) ? new Thickness(1) : new Thickness(2);
}

private static T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject
{
    DependencyObject parent = VisualTreeHelper.GetParent(dependencyObject);
    if (parent == null)
        return null;

    T parentT = parent as T;
    return parentT ?? FindParent<T>(parent);
}

XAML: XAML:

<DataTemplate>
    <Grid
        x:Name="itemThumbnail"
        Loaded="itemThumbnail_Loaded"
        ...

FindParent<AdaptiveGridView>(grid) also works if you don't need the GridViewItem . FindParent<AdaptiveGridView>(grid)如果您不需要GridViewItem也可以使用。

I have to say that it is difficult to get the gridviewItem through the pass value in converter.不得不说,通过converter中的pass值获取gridviewItem是很困难的。

I suggest you could try another way instead, you could set a property named Ness that binds to BorderThickness , then write logic in its get method.我建议您可以尝试另一种方式,您可以设置一个名为 Ness 的属性,该属性绑定到BorderThickness ,然后在其 get 方法中编写逻辑。 So that you could get the binding value directly that you expected.这样您就可以直接获得您期望的绑定值。

Xaml code: Xaml 代码:

<Grid  x:Name="itemThumbnail"  BorderBrush="Gray" BorderThickness="{x:Bind Ness}" Background="White">

Code behind:后面的代码:

public sealed partial class MainPage : Page
    {
        public static int index = 0;
        ……
}
public class SampleOrder
    {      
        private Thickness ness;
        public Thickness Ness
        {
            get
            {
                if (MainPage.index % 2 == 0)
                {
                    ness = new Thickness(1);
                }
                else
                {
                    ness = new Thickness(3);
                }
                MainPage.index++;
                return ness;
            }
        }        
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM