UWP TextBlock文本不变

时间:2018-08-07 23:50:17

标签: c# xaml uwp xbind

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

我有一个绑定到ObservableCollection的{​​{1}}。测试ListView正确显示。我也有一个使用Show绑定的Show SelectedShow。当我在Mode.TwoWay中选择其他Show时,会收到相应的调试语句。

当我在ListView中单击不同的TextBlock时,Text的{​​{1}}不会改变。

有什么想法吗?

MainPage.xaml:

Show

MainPage.xaml.cs:

ListView

Show.cs:

<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>

1 个答案:

答案 0 :(得分:0)

您还需要为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列表以及该VM中的SelectedShow和Implement INotifyPropertyChanged。

相关问题