[英]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屬性。 問題是Binding
對HabitacionControl
的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.