【问题标题】:UWP TextBlock Text doesn't changeUWP TextBlock 文本不会改变
【发布时间】:2019-01-15 03:21:26
【问题描述】:

我的目标是制作一个可供用户选择的可选节目列表,然后在右侧面板中编辑有关它的数据。

我有一个绑定到ListViewObservableCollection。测试Shows 显示正确。我还有一个使用Mode.TwoWay 绑定的Show SelectedShow。当我在ListView中选择不同的Show时,我会收到相应的调试语句。

当我在ListView 中单击不同的Shows 时,TextBlockText 不会改变。

有什么想法吗?

MainPage.xaml:

<Page
    x:Class="PlexHelper.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:PlexHelper"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="2*"></ColumnDefinition>
            <ColumnDefinition Width="6*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Vertical">
            <TextBox HorizontalAlignment="Stretch" Text="" PlaceholderText="Search shows..."/>
            <ListView ItemsSource="{x:Bind Shows}" SelectedItem="{x:Bind SelectedShow, Mode=TwoWay}" SelectionChanged="OnSelectionChanged">
                <ListView.ItemTemplate>
                    <DataTemplate x:DataType="local:Show">
                        <TextBlock Text="{x:Bind Name, Mode=OneWay}"/>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackPanel>
        <TextBlock Grid.Column="1" FontSize="50" HorizontalAlignment="Center" VerticalAlignment="Center" Text="{x:Bind SelectedShow.Name, Mode=OneWay}"></TextBlock>
    </Grid>
</Page>

MainPage.xaml.cs:

using System.Collections.ObjectModel;
using System.Diagnostics;
using Windows.UI.Xaml.Controls;

namespace PlexHelper
{
    public sealed partial class MainPage : Page
    {
        public ObservableCollection<Show> Shows { get; } = new ObservableCollection<Show>();
        public Show SelectedShow { get; set; }

        public MainPage()
        {
            this.InitializeComponent();
            Shows.Add(new Show("Show 1"));
            Shows.Add(new Show("Show 2"));
            Shows.Add(new Show("Show 3"));
            SelectedShow = Shows[0];
        }

        private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Debug.WriteLine("now selected: " + SelectedShow.Name);
        }
    }
}

显示.cs:

using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace PlexHelper
{
    public class Show : INotifyPropertyChanged
    {
        private string _name;
        public string Name
        {
            get => _name;
            set
            {
                if (_name != value)
                {
                    _name = value;
                    OnPropertyChanged();
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public Show(string name = "Default Show Name")
        {
            Name = name;
        }

        public void OnPropertyChanged([CallerMemberName] string propertyName = null) =>
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

【问题讨论】:

    标签: c# xaml uwp xbind


    【解决方案1】:

    您还需要为 SelectedShow 引发 PropteryChanged 事件,以便在 UI 上反映它。

    您的MainPlage 应如下所示:

    public sealed partial class MainPage : Page, INotifyPropertyChanged
            {
                public ObservableCollection<Show> Shows { get; } = new ObservableCollection<Show>();
    
                private Show _selectedShow;
                public Show SelectedShow
                {
                    get => _selectedShow;
                    set
                    {
                        if (_selectedShow != value)
                        {
                            _selectedShow = value;
                            OnPropertyChanged();
                        }
                    }
                }
    
                public MainPage()
                {
                    this.InitializeComponent();
                    Shows.Add(new Show("Show 1"));
                    Shows.Add(new Show("Show 2"));
                    Shows.Add(new Show("Show 3"));
                    SelectedShow = Shows[0];
                }
    
                private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
                {
                    Debug.WriteLine("now selected: " + SelectedShow.Name);
                }
    
                public event PropertyChangedEventHandler PropertyChanged;
    
                public void OnPropertyChanged([CallerMemberName] string propertyName = null) =>
                   PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
     }
    

    我最好建议创建一个 ViewModel,其中包含 Show 列表以及 SelectedShow 并在该 VM 中实现 INotifyPropertyChanged。

    【讨论】:

    • 非常感谢!我知道我忘记了什么。
    • 我很高兴它有帮助。
    猜你喜欢
    • 1970-01-01
    • 2017-11-07
    • 1970-01-01
    • 1970-01-01
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多