在WINUI3中获取元素根的ViewModel上下文
我有一个包含listView x的页面:绑定到我的ViewModel中的对象。该对象包含一个对象列表(时间戳),其中包含包含另一个对象列表的主题列表。我将在2个列表视图中介绍数据,一个在彼此内部。
<ListView
x:Name="primaryList" // for exemplification purposes
ItemsSource="{x:Bind ViewModel.VideoProject.TimeStamps, Mode=OneWay}"
ItemClick='{x:Bind ViewModel.ListViewTimeStamps_ItemClick, Mode=OneWay}'>
ListView包含另一个ListView的数据定位板
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Spacing="5">
<TextBlock Text="{Binding Id}"
FontSize="15"
HorizontalAlignment="Left"
FontWeight="Bold" />
<ListView ItemsSource="{Binding Subjects}"
x:Name="secondaryList"
SelectionMode="Multiple">
<ListView.ItemTemplate>
....
,第二个ListView之后是另一个相同的结构。
我的目标是将第二个ListView ItemClickevent绑定到我的ViewModel内部的ListViewTimestamps_itemclick方法,因为我需要SecondaryListView所包含的对象中包含的数据(主题)。 我可以尝试将数据模板上下文设置为ViewModel,但它会破坏主题绑定。
我发现了有关此主题的很多问题,但与WPF不同的是,没有祖先来吸引树木参考。
obs: 我的项目基于模板模型,该模型以ViewModel作为属性创建XAML .C。我也没有在XAML页面上设置DataContext,因为我可以X:正式将视图模型绑定到页面元素的情况下,而无需显式设置。
有没有使用附加属性的方法可以完成的方法? 谢谢。
I have a page which contains a ListView x:bound to an object in my ViewModel. This object contains a list of objects (timestamps) that contains a list of Subjects that contains a list of another objects. I'm presenting the data in 2 list views, one inside another.
<ListView
x:Name="primaryList" // for exemplification purposes
ItemsSource="{x:Bind ViewModel.VideoProject.TimeStamps, Mode=OneWay}"
ItemClick='{x:Bind ViewModel.ListViewTimeStamps_ItemClick, Mode=OneWay}'>
The ListView contains a DataTemplate for another ListView
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Spacing="5">
<TextBlock Text="{Binding Id}"
FontSize="15"
HorizontalAlignment="Left"
FontWeight="Bold" />
<ListView ItemsSource="{Binding Subjects}"
x:Name="secondaryList"
SelectionMode="Multiple">
<ListView.ItemTemplate>
....
And the second ListView is followed by another same structure.
My goal is to bind the second ListView ItemClickEvent to ListViewTimeStamps_ItemClick method inside my ViewModel, because I need the data contained in the object that secondaryListView holds (Subject).
I could try to set the Data Template Context to the ViewModel but it would break the Subject bind.
I found a ton of questions about this topic but differently from WPF there's not AncestorType to catch the up tree reference.
Obs:
My project is based on the Template Model which creates the XAML .cs with the ViewModel as a Property. I also haven't set the DataContext on the XAML page because I can x:bind normally my view model to the page elements without explicit set.
Is there a way to accomplish without using Attached Properties?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于没有支持
Ancestortype
在Winui中设置reachivesource
的属性,因此无需编写某些代码即可在纯XAML中完成此操作。您可以按建议实现附带的bevaiour,并举例说明在这里:
不,不在XAML中。
Since there is no support for setting the
AncestorType
property of aRelativeSource
in WinUI, there is no way to accomplish this in pure XAML without writing some code.You could implement an attached bevaiour as suggested and exemplified here:
No. Not in XAML.