无法在uwp应用程序vs2017中绑定 - 同样,ListView不滚动

时间:2017-11-03 11:40:51

标签: c# xaml listview uwp-xaml

我无法使用VS2017将对象绑定到UWP应用中的页面。我可以绑定一个集合,但不能绑定一个对象。我的代码:

User.cs

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)); }
        }
    }
}

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>

MainPage.xaml.cs中

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;
        }
    }
}

如果我取消注释如下所示的两行,我会得到我正在寻找的效果

private void UserList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (UserList.SelectedItem is User user)
    {
        UserViewModel = user;

        //UNCOMMENTED
        NameTextBox.Text = $"{user.Name}";
    }
}

private void NameTextBox_TextChanged(object sender, TextChangedEventArgs e)
{

    //UNCOMMENTED
    UserViewModel.Name = NameTextBox.Text;
}

但是,这不是我需要的。在我正在开发的实际应用程序中,将有一个包含多个字段的表单,需要双向绑定到UserViewModel实例。对于表单上的每个字段,我都不想使用TextChanged事件处理程序(或FocusLost,无论如何)来填充表单。

我的第二个问题是ListView没有滚动。

提前致谢。

1 个答案:

答案 0 :(得分:1)

您应该能够使用绑定将TextBox绑定到UserViewModel。我们可以将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}" />

代码背后:

private void UserList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (UserList.SelectedItem is User user)
    {
        UserViewModel = user; 
        NameTextBox.DataContext = UserViewModel;
    }
}

形成代码,您已设置Height =&#34; 1000&#34;到ListBox。当物品&#39;高度大于1000,可以滚动ListBox。如果没有,则无法滚动。您应该可以删除Height