【问题标题】:Binding click handler inside DataTemplate在 DataTemplate 中绑定点击处理程序
【发布时间】:2016-03-25 08:06:28
【问题描述】:

我想在 DataTemplate 元素内绑定一个 gridview-click。我必须使用 HubSections 所必需的 DataTemplate 元素。

如果我在 DataTemplate 元素中绑定此命令,则会收到错误消息:

“对象引用未设置为对象的实例”

<Frame x:Name="frame">
  <Hub VerticalAlignment="Center" HorizontalAlignment="Center"  >
     <HubSection Width="250" x:Name="section1"  IsHeaderInteractive="True" >
                    <DataTemplate>
                        <GridView IsItemClickEnabled="True" ItemClick="{x:Bind MainPageVM.click}">
                            <RelativePanel>
                                <Image x:Name="image" Source="Assets/1.png" Width="Auto" Height="250"  />
                                <TextBlock x:Name="page1" Text="page1" />
                            </RelativePanel>
                        </GridView>
                    </DataTemplate>
                </HubSection>

                //....
         </Hub>             
</Frame>

但是,如果我在 DataTemplate 元素之外绑定相同的处理程序(例如 HubSectionHeaderClick),它可以正常工作。

但我不明白为什么?我有什么办法来解决这个问题?

编辑: 这里是带有 ElementName 的 XAML:

<Hub VerticalAlignment="Center" HorizontalAlignment="Center" x:Name="ThisIsHub" >

和绑定:

Binding ElementName=ThisIsHub, Path=DataContext.click

得到错误:

无法解析“对象”类型的数据上下文中的属性“点击”

【问题讨论】:

  • This 问题貌似类似,但是UWP/Win10下不行。
  • 我猜dstatemplate的数据上下文不同
  • 为集线器控件命名。使用此绑定 {Binding ElementName=''Hubrleelement'',Path="DataContext.clickcommand}.. 假设您的中心元素 dstacontext 指的是 MsinPageVM。让我知道它是否有效
  • 不工作,因为没有找到 ElementName,虽然我重命名了它。我认为你是对的 DataTemplate 的 DataContext 是不同的。但为什么?这是默认行为吗?
  • 如果我将命令绑定到代码隐藏方法,它可以工作。但不在 ViewModel 中...

标签: c# xaml mvvm win-universal-app


【解决方案1】:

在 GridView/ListView 上绑定 click 事件时,我总是按照本教程进行操作(这是链接:https://marcominerva.wordpress.com/2013/03/07/how-to-bind-the-itemclick-event-to-a-command-and-pass-the-clicked-item-to-it/

首先你必须创建一个定义附加属性的类:

public static class ItemClickCommand 
{ 
    public static readonly DependencyProperty CommandProperty = 
    DependencyProperty.RegisterAttached("Command", typeof(ICommand), 
    typeof(ItemClickCommand), new PropertyMetadata(null, OnCommandPropertyChanged));

    public static void SetCommand(DependencyObject d, ICommand value) 
    { 
       d.SetValue(CommandProperty, value); 
    }

    public static ICommand GetCommand(DependencyObject d) 
    { 
        return (ICommand)d.GetValue(CommandProperty); 
    }

    private static void OnCommandPropertyChanged(DependencyObject d, 
    DependencyPropertyChangedEventArgs e) 
    { 
        var control = d as ListViewBase; 
        if (control != null) 
            control.ItemClick += OnItemClick; 
    }

    private static void OnItemClick(object sender, ItemClickEventArgs e) 
    { 
        var control = sender as ListViewBase; 
        var command = GetCommand(control);

        if (command != null && command.CanExecute(e.ClickedItem)) 
            command.Execute(e.ClickedItem); 
    } 
}

然后在 XAML 中简单地以这种方式使用它(它适用于 ListView/GridView):

helpers:ItemClickCommand.Command="{Binding Click}

其中helpers 是您定义附加属性的命名空间

然后,在您的视图模型中,定义命令:

private RelayCommand<MyItem> click; 
public RelayCommand<MyItem> Click
{ 
    get
    { 
        if (click== null) 
        { 
            click= new RelayCommand<MyItem>( 
                (item) => 
                {                            
                    //do something with your item 
                }); 
        }

        return click; 
    } 
}

让我知道它是否有效!

【讨论】:

  • 谢谢,但问题不在于 ItemView,我也尝试绑定单击按钮(在 DataTemplate 内),但这也不起作用。所以我猜想 DataContext 是不同的。
【解决方案2】:

我认为您的视图模型已绑定到 hubsection DataContext,因此当您将命令放入 HubSectionHeaderClicked 事件时它可以工作。所以试试这个

使用此绑定

 {Binding ElementName=section1,Path="DataContext.clickcommand}

让我知道它是否有效

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-29
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多