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

查看:76
本文介绍了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属性。问题是 Binding HabitacionControl 而不是其父项的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>

这就是为什么您不为UserControls创建视图模型的原因。您已经打破了数据绑定的工作方式。视图模型必须通过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 属性。您如何绑定到它?您的视图模型将流入 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天全站免登陆