【发布时间】:2019-08-21 17:27:00
【问题描述】:
我是绑定新手,当我在 ListView 中选择一个项目时,我无法显示我的 RecipeName。
我正在尝试将我选择的项目绑定到我的TextBox。我确实让我的项目在 ListView 中正确显示,但是当我选择一个项目时它没有在我的 TextBox 中预览。
我在这里做错了什么?
带有 ListView 和文本框的 Xaml
<ListView Grid.Column="0"
Name="listOfRecipes"
Margin="10,10,10,240"
Height="150"
ItemsSource="{Binding Path=Recipe}"
SelectedItem="{Binding Path=RecipeName, Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=RecipeName, Mode=TwoWay}" /> // This lists my items in the ListView Correctly
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<TextBox Text="{Binding Path=RecipeName, Mode=TwoWay}" Grid.Column="1" Height="50" Margin="10,10,-10,340"/> // This doesn't preview the selected item.
我的班级:
public partial class ViewAll : Page, INotifyPropertyChanged
{
private Recipe _recipe;
public Recipe Recipe
{
get { return _recipe; }
set
{
if(_recipe != value)
{
_recipe = value;
OnPropertyChanged("Recipe");
}
}
}
public ViewAll()
{
InitializeComponent();
LoadItemTemplate();
}
public void LoadItemTemplate()
{
mrydendbEntities dbe = new mrydendbEntities();
listOfRecipes.ItemsSource = dbe.Recipe.ToList();
listOfRecipes.SelectedItem = dbe.Recipe.First();
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
【问题讨论】: