【发布时间】:2019-03-09 11:11:00
【问题描述】:
我有一个 UWP 页面,其中包含一个带有文本框的表单和一个 ListView 控件。 ListView 控件绑定到产品集合。我希望绑定的文本框应该显示有关在列表视图中选择的产品的信息。
public class Product: INotifyPropertyChanged
{
public int ProductID { get; set; }
private string name;
public string Name {
get { return name; }
set
{
if (name==value)
return;
name = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(nameof(Name)));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public Product(int pid, string name)
{
ProductID = pid;
Name = name;
}
}
}
XAML 如下:
<TextBox x:Name="txtProductId" Grid.Row="1" Grid.Column="1"
Text="{x:Bind CurrentProduct.ProductID}"/>
<TextBox x:Name="txtProductName" Grid.Row="2" Grid.Column="1"
Text="{x:Bind CurrentProduct.Name}" />
<ListView x:Name="lstProducts" Grid.Row="3" Grid.ColumnSpan="2"
ItemsSource="{x:Bind ProductList}"
SelectedItem="{x:Bind CurrentProduct, Mode=TwoWay}"
ItemTemplate="{StaticResource lstDataTemplate}"
>
</ListView>
以下代码在 Page_Loaded 上执行:
CurrentProduct = Products[0];
DataContext = CurrentProduct;
ListView 绑定到 ProductList(类型 ObservableCollection)。我注意到通过在单步模式下执行应用程序,CurrentProduct 的值正在发生变化,但我认为由于它是引用而不是 DataContext 的属性发生变化,因此永远不会触发 PropertyChanged 事件并且 TextBox 不会更新以显示 CurrentProduct 的名称。
我不知道如何继续,任何帮助将不胜感激。
【问题讨论】:
标签: data-binding uwp