属性更改时UI不更新

时间:2016-08-05 18:58:36

标签: c# xaml uwp inotifypropertychanged

我有实现INotifyPropertyChanged的联系人类

    public class Contact : INotifyPropertyChanged
    {
        public Contact(Contact contact)
        {
            this.Username = contact.Username;
            this.GUID = contact.GUID;
            this.Msg = contact.Msg;
            this.Ring = contact.Ring;
        }

        private string username;
    public string Username
    {
        get { return username; }
        set
        {
            username = value;
            NotifyPropertyChanged(nameof(Username));
        }
    }

    public Guid GUID { get; set; }
    public bool Msg { get; set; }

        private bool ring;
        public bool Ring
        {
            get { return ring; }
            set
            {
                ring = value;
                NotifyPropertyChanged(nameof(Ring));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

这是主页

    public sealed partial class MainPage : Page
        {
            public ObservableCollection<Contact> Contacts = new ObservableCollection<Contact>();

            public MainPage()
            {
                this.InitializeComponent();
                Contacts.Add(new Contact("Contact001", Guid.NewGuid(), false, false));
                Contacts.Add(new Contact("Contact002", Guid.NewGuid(), false, false));
            }

            private void AddContactButton_Click(object sender, RoutedEventArgs e)
            {
                Contacts.Add(new Contact("ContactN", Guid.NewGuid(), false, false));
            }

            private void ContactsListView_ItemClick(object sender, ItemClickEventArgs e)
            {
                Contact clickedContact = (Contact)e.ClickedItem;
                int index = Contacts.IndexOf(clickedContact);
                Contacts.ElementAt(index).Username = "Qwerty";
            }
        }
    }

这是 XAML

<Page
    x:Class="ContactsListBinding.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ContactsListBinding"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:data="using:ContactsListBinding.Models"
    xmlns:namespace="ContactsListBinding.Models">

    <Page.Resources>
        <data:MessageToImageConverter x:Key="MessageToImageConverter" />
        <data:RingToImageConverter x:Key="RingToImageConverter" />
    </Page.Resources>

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" Grid.Column="0">
            <Button Name="AddContactButton" Content="Add Contact" Click="AddContactButton_Click" />
            <CheckBox Name="MessageMeCheckBox" Content="Message me" />
            <CheckBox Name="DeleteMeCheckBox" Content="Delete me" />
        </StackPanel>

        <ListView Grid.Row="1" Grid.Column="0" Name="ContactsListView" 
                          IsItemClickEnabled="True" 
                          ItemClick="ContactsListView_ItemClick"
                          ItemsSource="{x:Bind Contacts}">
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="data:Contact">
                    <Grid Width="500">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Row="0" Grid.Column="0" Text="{x:Bind Username}" VerticalAlignment="Center" />
                        <Image Grid.Row="0" Grid.Column="1" Width="15" Height="15" Source="{x:Bind Msg, Converter={StaticResource MessageToImageConverter}}" />
                        <Image Grid.Row="0" Grid.Column="2" Width="15" Height="15" Source="{x:Bind Ring, Converter={StaticResource RingToImageConverter}}" />
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Page>

因此,当我点击某个项目时,我将它的Ring属性更改为true。我已调试,它正在变为true。唯一的问题是我的UI没有更新。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

好的伙计们,我终于开始工作了。似乎应该将绑定模式设置为OneWay,而不是默认的OneTime。例如,正确的xaml应为:

<TextBlock Grid.Row="0" Grid.Column="0" Text="{x:Bind Username, Mode=OneWay}" VerticalAlignment="Center" />

非常感谢你的帮助! :)