c# - 属性更改时 UI 不更新

标签 c# xaml uwp inotifypropertychanged

我有实现 INotifyPropertyChanged 的​​ Contact 类:

    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。唯一的问题是我的用户界面没有更新。有什么想法吗?

最佳答案

好的伙计们,我终于让它工作了。似乎应该将 Binding Mode 设置为 OneWay,而不是默认的 OneTime。例如正确的 xaml 应该是:

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

非常感谢您的帮助! :)

关于c# - 属性更改时 UI 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38796122/

相关文章:

c# - Sql 数据库中的字符串是问号 C#

c# - 我如何知道何时/什么正在加载某些程序集?

xaml - 如果 ListView 中的数据为空,则隐藏行

c# - 如何使用绑定(bind)嵌套 View ?

c# - 通用Windows应用程序蓝牙支持

c# - uwp xaml 解析失败,类库 dll

c# - 使用 ASP.NET MVC 分页在网页上显示日志文件信息

c# - ComboBox 中的选定属性未显示

c# - 无法加载文件或程序集 'System.Private.CoreLib...'

c# - 如何在保持趋势的同时删除常规间隔中的 n% 的列表?