【问题标题】:Bind a grouped Collection in Xaml在 Xaml 中绑定分组集合
【发布时间】:2012-11-12 16:25:01
【问题描述】:

我搜索了一下,但我找到的信息不是我需要的。所以我决定问你们所有人——我确定这是一个新手问题,但我真的不明白。 开始吧: 我有一个 DataSource,它是一个分组的可观察集合。目前我有 2 组不同数量的项目。两个组和项属于同一个公共基础:

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;
    }

这是模型的构造函数。 在 ViewModel 我填充它。 现在我想将带有命令的 ItemClick 绑定到我的 ViewModel。我喜欢这个(只是一小部分):

<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>

但是现在问题来了。在“Binding UniqueId”中,它说 DataContext 是我的 ViewModel,所以我无法将它连接到模型的属性。看着 Page.DataContext 我告诉 XAML 你使用我的 ViewModel 作为 DataContext。我想这是正确的。但是我怎样才能访问模型属性? 我试过这样做(将我的模型定义为 DataModel):

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

但正如我之前猜测的那样,它不起作用 - 作为参数,我得到了 null。

我会感谢任何帮助,因为正如我在帖子开头所说的:我真的不明白......

【问题讨论】:

    标签: xaml collections windows-runtime eventtocommand


    【解决方案1】:

    您不能以这种方式使用EventToCommandBehavior - 它的作者在comments 中也说明了这一点。

    在这种情况下,我使用以下附加属性:

    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);
        }
    }
    

    这是您将命令绑定到它的方式:

    <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}"
        >
    

    我想如果你真的想从附加的属性创建一个行为并不难。

    【讨论】:

      猜你喜欢
      • 2013-03-01
      • 2018-10-13
      • 1970-01-01
      • 2016-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-10
      相关资源
      最近更新 更多