C# 无法在uwp应用vs2017中绑定-同时,ListView未滚动
我无法使用VS2017将对象绑定到UWP应用程序中的页面。我可以绑定集合,但不能绑定单个对象。我的代码: User.csC# 无法在uwp应用vs2017中绑定-同时,ListView未滚动,c#,xaml,listview,uwp-xaml,C#,Xaml,Listview,Uwp Xaml,我无法使用VS2017将对象绑定到UWP应用程序中的页面。我可以绑定集合,但不能绑定单个对象。我的代码: User.cs using System.ComponentModel; namespace Binding { public class User : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected vo
using System.ComponentModel;
namespace Binding
{
public class User : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private string _name = "";
public User(string name)
{
Name = name;
}
public string Name
{
get { return _name; }
set { _name = value ?? ""; OnPropertyChanged(nameof(Name)); }
}
}
}
using System.Collections.ObjectModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace Binding
{
public sealed partial class MainPage : Page
{
public ObservableCollection<User> _users { get; } = new ObservableCollection<User>();
public User UserViewModel { get; set; }
public MainPage()
{
InitializeComponent();
UserList.ItemsSource = _users;
}
private void addBtn_Click(object sender, RoutedEventArgs e)
{
_users.Add(new User("Name" + _users.Count));
}
private void UserList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (UserList.SelectedItem is User user)
{
UserViewModel = user;
//NameTextBox.Text = $"{user.Name}";
}
}
private void NameTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
//UserViewModel.Name = NameTextBox.Text;
}
}
}
MainPage.xaml
<Page
x:Class="Binding.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button
x:Name="addBtn"
Grid.Row="0"
Grid.Column="0"
Click="addBtn_Click"
Content="Add"/>
<ListBox
x:Name="UserList"
Grid.Row="1"
Grid.Column="0"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollMode="Auto"
SelectionChanged="UserList_SelectionChanged"
Height="1000"
>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBox
x:Name="NameTextBox"
Grid.Row="0"
Grid.Column="1"
TextChanged="NameTextBox_TextChanged"
Text="{x:Bind Path=UserViewModel.Name, Mode=TwoWay}"
/>
</Grid>
</Page>
然而,这不是我需要的。在我正在开发的实际应用程序中,将有一个带有多个字段的表单,需要双向绑定到UserViewModel实例。我不想为表单上的每个字段都添加TextChanged事件处理程序(或FocusLost,无论什么)
我的第二个问题是ListView不能滚动
提前感谢。您应该能够使用绑定将
文本框
绑定到用户视图模型
。我们可以将PropertyChanged
设置为UpdateSourceTrigger
当我们将UpdateSourceTrigger
设置为PropertyChanged
时,只要绑定目标值发生更改,绑定源就会更新。这由绑定系统自动检测
x:Bind没有UpdateSourceTrigger
属性,它只在失去焦点时触发
例如:
<TextBox
x:Name="NameTextBox"
Grid.Row="0"
Grid.Column="1"
TextChanged="NameTextBox_TextChanged"
Text="{Binding Name,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
在您的代码中,您已将高度设置为“1000”。当项目高度大于1000时,可以滚动列表框
。如果没有,则无法滚动。您应该能够删除高度
<TextBox
x:Name="NameTextBox"
Grid.Row="0"
Grid.Column="1"
TextChanged="NameTextBox_TextChanged"
Text="{Binding Name,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
private void UserList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (UserList.SelectedItem is User user)
{
UserViewModel = user;
NameTextBox.DataContext = UserViewModel;
}
}