[VB] X:将Datatemplate指针事件绑定到viewmodel事件 [英] [VB]X:Bind Datatemplate pointerpressed event to viewmodel event

查看:99
本文介绍了[VB] X:将Datatemplate指针事件绑定到viewmodel事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<Page
    x:Class="App3.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App3"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:data="using:App3.MyViewModel"
    mc:Ignorable="d" DataContextChanged="Page_DataContextChanged">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <ListView Grid.Column="0" ItemsSource="{x:Bind m_ViewModel.ListItems}" >
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="local:lvitem">
                    <Button Click="{x:Bind Poke}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <TextBlock Grid.Column="1" Text="{x:Bind m_ViewModel._Name, Mode=TwoWay}"/>
    </Grid>
</Page>


Imports Windows.UI
Namespace MyViewModel
    Public Class MyViewModel
        Implements INotifyPropertyChanged
        Private Sub NotifyPropertyChanged(Optional propertyName As String = "")
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
        End Sub

        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
        Private Name As String
        Private Olditem As ListViewItem
        Private List_Items As New ObservableCollection(Of lvitem)
        Public Property Old_Item As ListViewItem
            Get
                Return Olditem
            End Get
            Set(value As ListViewItem)
                Olditem = value
                NotifyPropertyChanged()
            End Set
        End Property

        Public Property _Name As String
            Get
                Return Name

            End Get
            Set(value As String)
                Name = value
                NotifyPropertyChanged()
            End Set
        End Property
        Public Sub New()
            Name = "Cater"
            Dim lvi As New lvitem
            Dim lvi2 As New lvitem
            List_Items.Add(lvi)
            List_Items.Add(lvi2)
        End Sub

        Public Property ListItems As ObservableCollection(Of lvitem)
            Get
                Return List_Items
            End Get
            Set(value As ObservableCollection(Of lvitem))
                List_Items = value
            End Set
        End Property
        Public Sub ClickMe()
            _Name = "Clicked"


        End Sub
    End Class
End Namespace


' The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
''' <summary>
''' An empty page that can be used on its own or navigated to within a Frame.
''' </summary>
Public NotInheritable Class MainPage
    Inherits Page
    Public Property ViewModel() As MyViewModel.MyViewModel
        Get
            Return m_ViewModel
        End Get
        Set
            m_ViewModel = Value
        End Set
    End Property
    Private m_ViewModel As MyViewModel.MyViewModel
    Public Sub New()
        Me.InitializeComponent()
        ViewModel = New MyViewModel.MyViewModel
    End Sub
    Private Sub Rectangle_PointerPressed(sender As Object, e As PointerRoutedEventArgs)
    End Sub
End Class






Public Class lvitem
    Public Sub Poke(sender As Object, e As Object)
        Dim meint As Integer
        meint = 3
    End Sub
End Class




我正在尝试从我的datacontex中的按钮调用我的viewmodel中的事件,但它似乎没有编译。给我一个错误。欢迎任何帮助谢谢


I'm trying to call an event in my viewmodel from the button in my datacontex but it doesn't seem to compile. Gives me an error. Any help is welcomed thanks

推荐答案

您好  Twinnaz

数据绑定使用属性。为了获得成功,实现ICommand接口的属性更多读取

here 

the Data Binding working with properties. For getting success  implement ICommand interface with properties for more read here 


这篇关于[VB] X:将Datatemplate指针事件绑定到viewmodel事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆