S
S
Stanislav Silin2016-08-11 23:15:37
WPF
Stanislav Silin, 2016-08-11 23:15:37

Why is SelectedItem not working in ListView for UWP app?

There is the following view:

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DataContext="{d:DesignInstance local:RootViewModel}">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <TextBlock Text="{Binding Selected}" TextAlignment="Center"/>
        <GridView  Grid.Row="1"
                  SelectionMode="Single"
                  HorizontalAlignment="Center"
                  ItemsSource="{Binding Items}" 
                  SelectedItem="{Binding  Selected}" />
    </Grid>
</Page>

There is the following view model:
public class RootViewModel : INotifyPropertyChanged
    {

        public IEnumerable<string> Items
        {
            get;
        } = new[] { "1", "2", "3", };

        string selected;

        public string Selected
        {
            get
            {
                return this.selected;
            }
            set
            {
                this.selected = value;
                this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(this.Selected)));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

When I select a cell with a ListView, nothing happens to the Selected field in the view model (everything is normally highlighted on the UI). In WPF, code like this works great.
Does anyone have any ideas why this is happening?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vyacheslav Zolotov, 2016-08-12
@byme

Did you do bidirectional binding?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question