在WINUI3中获取元素根的ViewModel上下文

发布于 2025-01-19 16:54:04 字数 1132 浏览 6 评论 0原文

我有一个包含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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘心安 2025-01-26 16:54:04

由于没有支持Ancestortype在Winui中设置reachivesource的属性,因此无需编写某些代码即可在纯XAML中完成此操作。

您可以按建议实现附带的bevaiour,并举例说明在这里

public static class AncestorSource
{
    public static readonly DependencyProperty AncestorTypeProperty =
        DependencyProperty.RegisterAttached(
            "AncestorType",
            typeof(Type),
            typeof(AncestorSource),
            new PropertyMetadata(default(Type), OnAncestorTypeChanged)
    );

    public static void SetAncestorType(FrameworkElement element, Type value) =>
        element.SetValue(AncestorTypeProperty, value);

    public static Type GetAncestorType(FrameworkElement element) =>
        (Type)element.GetValue(AncestorTypeProperty);

    private static void OnAncestorTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        FrameworkElement target = (FrameworkElement)d;
        if (target.IsLoaded)
            SetDataContext(target);
        else
            target.Loaded += OnTargetLoaded;
    }

    private static void OnTargetLoaded(object sender, RoutedEventArgs e)
    {
        FrameworkElement target = (FrameworkElement)sender;
        target.Loaded -= OnTargetLoaded;
        SetDataContext(target);
    }

    private static void SetDataContext(FrameworkElement target)
    {
        Type ancestorType = GetAncestorType(target);
        if (ancestorType != null)
            target.DataContext = FindParent(target, ancestorType);
    }

    private static object FindParent(DependencyObject dependencyObject, Type ancestorType)
    {
        DependencyObject parent = VisualTreeHelper.GetParent(dependencyObject);
        if (parent == null)
            return null;

        if (ancestorType.IsAssignableFrom(parent.GetType()))
            return parent;

        return FindParent(parent, ancestorType);
    }
}

所以到目前为止尚未替换祖先?

不,不在XAML中。

Since there is no support for setting the AncestorType property of a RelativeSource 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:

public static class AncestorSource
{
    public static readonly DependencyProperty AncestorTypeProperty =
        DependencyProperty.RegisterAttached(
            "AncestorType",
            typeof(Type),
            typeof(AncestorSource),
            new PropertyMetadata(default(Type), OnAncestorTypeChanged)
    );

    public static void SetAncestorType(FrameworkElement element, Type value) =>
        element.SetValue(AncestorTypeProperty, value);

    public static Type GetAncestorType(FrameworkElement element) =>
        (Type)element.GetValue(AncestorTypeProperty);

    private static void OnAncestorTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        FrameworkElement target = (FrameworkElement)d;
        if (target.IsLoaded)
            SetDataContext(target);
        else
            target.Loaded += OnTargetLoaded;
    }

    private static void OnTargetLoaded(object sender, RoutedEventArgs e)
    {
        FrameworkElement target = (FrameworkElement)sender;
        target.Loaded -= OnTargetLoaded;
        SetDataContext(target);
    }

    private static void SetDataContext(FrameworkElement target)
    {
        Type ancestorType = GetAncestorType(target);
        if (ancestorType != null)
            target.DataContext = FindParent(target, ancestorType);
    }

    private static object FindParent(DependencyObject dependencyObject, Type ancestorType)
    {
        DependencyObject parent = VisualTreeHelper.GetParent(dependencyObject);
        if (parent == null)
            return null;

        if (ancestorType.IsAssignableFrom(parent.GetType()))
            return parent;

        return FindParent(parent, ancestorType);
    }
}

So no replacement so far for AncestorType?

No. Not in XAML.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文