属性更改时不更新UI



我有实现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没有更新。有什么想法吗?

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

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

非常感谢您的帮助!:(

相关内容

  • 没有找到相关文章

最新更新