C# UWP文本块文本不';不变
我的目标是制作一个可选择的显示列表,用户可以在其中选择一个,然后在右侧面板中编辑有关它的数据 我有一个绑定到C# UWP文本块文本不';不变,c#,xaml,uwp,xbind,C#,Xaml,Uwp,Xbind,我的目标是制作一个可选择的显示列表,用户可以在其中选择一个,然后在右侧面板中编辑有关它的数据 我有一个绑定到列表视图的ObservableCollection。测试Shows显示正确。我还有一个Show SelectedShow,它使用模式进行绑定。双向。当我在列表视图中选择不同的显示时,我会收到相应的调试语句 当我在列表视图中单击“不同的显示时,文本块的文本不会改变 有什么想法吗 MainPage.xaml: <Page x:Class="PlexHelper.MainPage"
列表视图的ObservableCollection
。测试Show
s显示正确。我还有一个Show SelectedShow
,它使用模式进行绑定。双向
。当我在列表视图
中选择不同的显示
时,我会收到相应的调试语句
当我在列表视图中单击“不同的显示时,文本块的文本不会改变
有什么想法吗
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>
您还需要为SelectedShow引发propertyChanged事件,以便在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));
}
公共密封部分类主页面:第页,INotifyPropertyChanged
{
公共ObservableCollection显示{get;}=new ObservableCollection();
私人秀(u selectedShow);;
公共节目精选节目
{
get=>\u selectedShow;
设置
{
如果(_selectedShow!=值)
{
_selectedShow=值;
OnPropertyChanged();
}
}
}
公共主页()
{
this.InitializeComponent();
显示。添加(新显示(“显示1”);
显示。添加(新显示(“显示2”));
显示。添加(新显示(“显示3”);
SelectedShow=显示[0];
}
SelectionChanged上的私有无效(对象发送方,SelectionChangedEventArgs e)
{
Debug.WriteLine(“现在选中:+SelectedShow.Name”);
}
公共事件属性更改事件处理程序属性更改;
public void OnPropertyChanged([CallerMemberName]字符串propertyName=null)=>
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
我建议您创建一个ViewModel,其中包含所选Show的显示列表,并在该VM中实现InotifyProperty更改。非常感谢!我知道我忘了一些东西。我很高兴这有帮助。
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));
}
}
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));
}