繁体   English   中英

XAML GridView ItemTemplate无法绑定到控件

[英]XAML GridView ItemTemplate not binding to control

我有一个带有ItemTemplate的GridView ,它包含一个Custom控件:

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

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

<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属性。

在运行时,我的gridview生成我的所有项目,但绑定到它的Ubicacion属性永远不会发生。

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

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

我的,请看这里:

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

有人卖给你一张脏肮脏的货物。 可能是其中一个混蛋告诉人们DataContext = this; 是个好主意。

对不起,相切。 现在看看这个:

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

我在看什么? 那是伪DataContext属性吗? 这是一个伪DataContext属性。 问题是BindingHabitacionControl的DataContext中的对象而不是其父对象起作用。 什么是HabitacionControl的DataContext?

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

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

我问你 - TextBox有TextBoxViewModel吗? 不。它具有绑定的Text属性。 你怎么绑它呢? 您的视图模型将流入TextBox.DataContext ,从而允许您将视图模型的属性绑定到TextBox上公开的属性。

还有其他hacky方法来解决这个问题,但最好的解决方案是首先不要让自己陷入这种状况。

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

不,这不会打破MVVM。 UI逻辑在代码隐藏中很好。

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

人们认为UserControlViewModel反模式是应该如何完成的。 它真的不是。 祝好运。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM