UWP TextBlock 文本不会改变
UWP TextBlock Text doesn't change
我的目标是制作一个 select 可用节目的列表,用户可以在其中选择一个节目,然后在右侧面板中编辑有关它的数据。
我有一个绑定到 ListView
的 ObservableCollection
。测试 Show
s 正确显示。我还有一个使用 Mode.TwoWay
绑定的 Show SelectedShow
。当我在ListView
中select一个不同的Show
时,我收到相应的调试语句。
当我在 ListView
中单击不同的 Show
时,TextBlock
的 Text
没有改变。
有什么想法吗?
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);
}
}
}
Show.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));
}
}
您还需要为 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,其中包含 Shows 列表以及 SelectedShow 并在该 VM 中实现 INotifyPropertyChanged。
我的目标是制作一个 select 可用节目的列表,用户可以在其中选择一个节目,然后在右侧面板中编辑有关它的数据。
我有一个绑定到 ListView
的 ObservableCollection
。测试 Show
s 正确显示。我还有一个使用 Mode.TwoWay
绑定的 Show SelectedShow
。当我在ListView
中select一个不同的Show
时,我收到相应的调试语句。
当我在 ListView
中单击不同的 Show
时,TextBlock
的 Text
没有改变。
有什么想法吗?
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);
}
}
}
Show.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));
}
}
您还需要为 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,其中包含 Shows 列表以及 SelectedShow 并在该 VM 中实现 INotifyPropertyChanged。