XAML GridView ItemTemplate 未绑定到控件 [英] XAML GridView ItemTemplate not binding to control

查看:38
本文介绍了XAML GridView ItemTemplate 未绑定到控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 GridView 和一个包含自定义控件的 ItemTemplate:

I have a GridView with an ItemTemplate that holds a Custom control:

<GridView
    ItemsSource="{Binding Ubicaciones.Ubicaciones}">
    <GridView.ItemTemplate>
        <DataTemplate>
            <ctr:HabitacionControl
                Width="70"
                Height="140"
                Ubicacion="{Binding}"/>
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>

这是我的自定义用户控件:

And here is my custom user control:

<UserControl
        x:Class="MySln.Mucama.Controls.HabitacionControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:MySln.Mucama.Controls"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="200"
        d:DesignWidth="97">
    <UserControl.DataContext>
        <local:HabitacionControlVM/>
    </UserControl.DataContext>
    <Grid>
        <RelativePanel>
            <Image x:Name="Puerta" Source="ms-appx:///Assets/Puerta.jpg"
                   Grid.RowSpan="5"/>
            <TextBlock Text="{Binding Ubicacion.StrNombreMesa,FallbackValue=####}"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center"
                       Foreground="AliceBlue"
                       FontWeight="ExtraBold"
                           RelativePanel.AlignHorizontalCenterWithPanel="True"/>
        </RelativePanel>
    </Grid>
</UserControl>

及其背后的代码:

public sealed partial class HabitacionControl : UserControl
{
    public HabitacionControl()
    {
        this.InitializeComponent();
    }

    public MyClass Ubicacion
    {
        get { return (MyClass)GetValue(UbicacionProperty); }
        set { SetValue(UbicacionProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Ubicacion.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty UbicacionProperty =
        DependencyProperty.Register("Ubicacion", typeof(MyClass), typeof(HabitacionControl), new PropertyMetadata(new PropertyChangedCallback(OnUbicacionChanged)));

    private static void OnUbicacionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
       //...
    }
}

现在我需要将每个 Ubicaciones.Ubicaciones 绑定到我的 Customcontrol 的 Ubicación 属性.

Now I need to bind each Ubicaciones.Ubicaciones to the Ubicación property of my Customcontrol.

在运行时,我的 gridview 生成了我的所有项目,但从未绑定到它的 Ubicacion 属性.

At run time my gridview generates all my items, but binding to its Ubicacion property never happens.

输出窗口中没有任何警告.

There aren't any warnings in the output window.

我错过了什么?还是做错了?

What I'm missing? Or doing wrong?

推荐答案

我的,看看这里:

<UserControl.DataContext>
    <local:HabitacionControlVM/>
</UserControl.DataContext>

有人卖给你一张肮脏、肮脏的商品.可能是那些跑来跑去告诉人们 DataContext = this; 是个好主意的混蛋之一.

Someone sold you a bill of dirty, filthy, goods. Probably one of those jerks who run around telling people DataContext = this; is a good idea.

对不起,切线.现在看看这个:

Sorry, tangent. Now look at this:

<ctr:HabitacionControl 
    Width="70" 
    Height="140" 
    Ubicacion="{Binding}"/>

我看到了什么?那是伪 DataContext 属性吗?这是一个伪 DataContext 属性.问题是 BindingHabitacionControl 的 DataContext 内的对象起作用,而不是它的父对象.HabitacionControl 的 DataContext 是什么?

What is that I'm seeing? Is that a pseudo-DataContext property? That's a pseudo-DataContext property. The problem is that the Binding works against the object within the DataContext of the HabitacionControl not its parent. And what's the DataContext of the HabitacionControl?

<UserControl.DataContext>
    <local:HabitacionControlVM/>
</UserControl.DataContext>

这就是为什么不为 UserControl 创建视图模型的原因.您已经破坏了数据绑定的工作方式.视图模型必须通过 DataContext 沿着可视化树向下流动.当您中断此流程时,您将失败.

And that's why you don't create view models for your UserControls. You have broken how data binding works. The view model must flow down the visual tree through the DataContext. When you interrupt this flow, you get fail.

让我问你——TextBox 有 TextBoxViewModel 吗?不.它有一个 Text 属性,you 绑定到它.你如何绑定它?您的视图模型流入 TextBox.DataContext,从而允许您将视图模型的属性绑定到在 TextBox 上公开的属性.

Let me ask you--does a TextBox have a TextBoxViewModel? No. It has a Text property that you bind to. How do you bind to it? Your view model flows into TextBox.DataContext, thus allowing you to bind properties of your view model to properties exposed on the TextBox.

还有其他黑客方法可以解决这个问题,但最好的解决方案是首先不要让自己陷入这种情况.

There are other hacky ways to get around this, but the best solution is to not get yourself into this situation in the first place.

您需要放弃 HabitacionControlVM 并在您的视图模型可以绑定的 UserControl 的表面上公开 DependencyProperties,提供您的 UserControl 需要的任何功能才能运行.将您的 UI 逻辑放在 HabitacionControl 的代码隐藏中.

You need to ditch that HabitacionControlVM and expose DependencyProperties on the surface of your UserControl that your view model can bind against, providing whatever your UserControl needs in order to function. Place your UI logic in the codebehind of HabitacionControl.

不,这不会破坏 MVVM.代码隐藏中的 UI 逻辑很好.

No, this doesn't break MVVM. UI logic is fine in the codebehind.

如果您的 HabitacionControlVM 正在执行不应该在代码隐藏中的繁重工作,那么只需将其重构为您的代码隐藏调用的类.

If your HabitacionControlVM is performing heavy lifting that really shouldn't be in the codebehind, then just refactor it into classes that your codebehind calls into.

人们认为 UserControlViewModel 反模式是应该如何做的.真的不是.祝你好运.

People think the UserControlViewModel anti-pattern is how it should be done. It really isn't. Good luck.

这篇关于XAML GridView ItemTemplate 未绑定到控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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