简体   繁体   English

在Xaml中绑定分组的Collection

[英]Bind a grouped Collection in Xaml

I've searched a bit, but the info I've found isn't what I need. 我进行了搜索,但是找到的信息不是我所需要的。 So I decided to ask you all - I'm sure it's a newbie question but i really don't get it. 所以我决定问大家-我确定这是一个新手问题,但我真的不明白。 Let's start: I have a DataSource which is a grouped observable collection. 让我们开始:我有一个DataSource,它是一个分组的可观察集合。 At the moment I've 2 groups with a different count of items. 目前,我有2个组,每个项目的计数不同。 The two groups and the items belong to the same common base: 这两个组和项目属于相同的共同基础:

public DataCommon(String uniqueId, String title, String subtitle, String imagePath, String description)
    {
        this._uniqueId = uniqueId;
        this._title = title;
        this._subtitle = subtitle;
        this._description = description;
        this._imagePath = imagePath;
    }

This is the constructor of the model. 这是模型的构造函数。 In the ViewModel I fill it. 在ViewModel中,将其填充。 Now I would like bind the ItemClick with a Command to my ViewModel. 现在,我想将带有命令的ItemClick绑定到我的ViewModel。 I do like this (only a short part): 我确实喜欢这样(只有一小部分):

<GridView
        x:Name="itemGridView"
        AutomationProperties.AutomationId="ItemGridView"
        AutomationProperties.Name="Grouped Items"
        Grid.RowSpan="2"
        Padding="116,137,40,46"
        ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"
        ItemTemplate="{StaticResource Standard250x250ItemTemplate}"
        SelectionMode="None"
        IsSwipeEnabled="false"
        IsItemClickEnabled="True"
        >
        <WinRtBehaviors:Interaction.Behaviors>
            <Win8nl_Behavior:EventToCommandBehavior Event="ItemClick" Command="ItemClickCommand" CommandParameter="{Binding UniqueId}"/>
        </WinRtBehaviors:Interaction.Behaviors>

But now the problem. 但是现在有问题了。 At the "Binding UniqueId" it's saying the DataContext is my ViewModel, so i can't connect it to the properties of the Model. 在“ Binding UniqueId”处,它表示DataContext是我的ViewModel,因此我无法将其连接到Model的属性。 Looked at the Page.DataContext i told XAML tu use my ViewModel as DataContext. 看了我告诉XAML的Page.DataContext,然后将ViewModel用作DataContext。 I guess this was correct. 我想这是正确的。 But how can I access the Model-properties? 但是,如何访问模型属性? I've tried to do it like this (defined my Model as DataModel): 我试图这样做(将我的模型定义为DataModel):

<WinRtBehaviors:Interaction.Behaviors>
            <Win8nl_Behavior:EventToCommandBehavior Event="ItemClick" Command="ItemClickCommand" CommandParameter="{Binding DataModel:SampleDataCommon.UniqueId}"/>
        </WinRtBehaviors:Interaction.Behaviors>

but as I guessed beforehand it didn't work - as parameter i get null. 但是正如我之前猜测的那样,它不起作用-作为参数,我得到了null。

I would be thankful for any help, because as i said at the beginning of the post: I really don't get it... 我会很感激您的帮助,因为正如我在帖子开头所说:我真的不明白...

You can't use EventToCommandBehavior in this way - this was also stated by its author in the comments . 您不能以这种方式使用EventToCommandBehavior这也由其作者在评论中指出

I'm using the following attached property in such cases: 在这种情况下,我将使用以下附加属性:

public static class ItemClickBehavior
{
    public static DependencyProperty ItemClickCommandProperty = DependencyProperty.RegisterAttached("ItemClickCommand",
                typeof(ICommand),
                typeof(ItemClickBehavior),
                new PropertyMetadata(null, OnItemClickCommandChanged));

    public static void SetItemClickCommand(DependencyObject target, ICommand value)
    {
        target.SetValue(ItemClickCommandProperty, value);
    }

    public static ICommand GetItemClickCommand(DependencyObject target) 
    {
        return (ICommand)target.GetValue(ItemClickCommandProperty);
    }

    private static void OnItemClickCommandChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
    {
        var element = target as ListViewBase;
        if (element != null)
        {
            // If we're putting in a new command and there wasn't one already
            // hook the event
            if ((e.NewValue != null) && (e.OldValue == null))
            {
                element.ItemClick += OnItemClick;
            }

            // If we're clearing the command and it wasn't already null
            // unhook the event
            else if ((e.NewValue == null) && (e.OldValue != null))
            {
                element.ItemClick -= OnItemClick;
            }
        }
    }

    static void OnItemClick(object sender, ItemClickEventArgs e)
    {
        GetItemClickCommand(sender as ListViewBase).Execute(e.ClickedItem);
    }
}

This is how you would bind a command to it: 这是将命令绑定到它的方式:

<GridView
    x:Name="itemGridView"
    AutomationProperties.AutomationId="ItemGridView"
    AutomationProperties.Name="Grouped Items"
    Grid.RowSpan="2"
    Padding="116,137,40,46"
    ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"
    ItemTemplate="{StaticResource Standard250x250ItemTemplate}"
    SelectionMode="None"
    IsSwipeEnabled="false"
    IsItemClickEnabled="True"
    itbmb:ItemClickBehavior.ItemClickCommand="{Binding ItemClickCommand}"
    >

I guess it wouldn't be all that difficult to create a behavior from the attached property if you really wanted to. 我想,如果您确实想要的话,从附加属性创建行为并不是那么困难。

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

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