Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF -- property is null when next page is shown
    text
    copied!<p>I'm having an issue where on my listview, when I select an item, the row highlight and the value for the property does get set. But when I click on the button to show the second page, the listview isnt highlighted/selected from the previous. The property is null. The two listview on each page reference the same properties for itemsource and selecteditem. Can anyone help me on why second page doesnt trigger property?</p> <p>MainWindow.xaml</p> <pre><code> &lt;UserControl.DataContext&gt; &lt;vm:MainViewModel /&gt; &lt;/UserControl.DataContext&gt; &lt;Grid&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="auto"&gt;&lt;/ColumnDefinition&gt; &lt;ColumnDefinition Width="5"&gt;&lt;/ColumnDefinition&gt; &lt;ColumnDefinition Width="auto"&gt;&lt;/ColumnDefinition&gt; &lt;ColumnDefinition Width="5"&gt;&lt;/ColumnDefinition&gt; &lt;ColumnDefinition Width="auto"&gt;&lt;/ColumnDefinition&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Button Name="btnPrevious" Content="&amp;lt;" Grid.Column="0" Height="300" Click="btnPrevious_Click"&gt;&lt;/Button&gt; &lt;Grid Width="310" Height="300" Grid.Column="2"&gt; &lt;Frame x:Name="UserControlContainer" NavigationUIVisibility="Hidden" Width="310" /&gt; &lt;/Grid&gt; &lt;Button Name="btnNext" Content="&amp;gt;" Grid.Column="4" Height="300" Click="btnNext_Click"&gt;&lt;/Button&gt; &lt;/Grid&gt; </code></pre> <p>MainWindow.xaml.cs</p> <pre><code>public partial class MainWindow : UserControl { private FirstPage fPage; private SecondPage sPage; private static int oldIndex = 1; public FirstPage FPage { get { if (fPage == null) fPage = new FirstPage(); return fPage; } set { fPage = value; } } public SecondPage SPage { get { if (sPage == null) sPage = new SecondPage(); return sPage; } set { sPage = value; } } public MainWindow() { InitializeComponent(); UserControlContainer.Source = new Uri("Views\\FirstPage.xaml", UriKind.Relative); } private void btnPrevious_Click(object sender, RoutedEventArgs e) { var content = UserControlContainer.Content as UserControl; var targetUserControl = this.FPage as UserControl; targetUserControl.DataContext = this.DataContext; this.UserControlContainer.NavigateToControl(targetUserControl, oldIndex, 2); oldIndex = 2; } private void btnNext_Click(object sender, RoutedEventArgs e) { var content = UserControlContainer.Content as UserControl; var targetUserControl = this.SPage as UserControl; targetUserControl.DataContext = this.DataContext; this.UserControlContainer.NavigateToControl(targetUserControl, oldIndex, 1); oldIndex = 1; } } </code></pre> <p>MainViewModel.cs</p> <pre><code>public class MainViewModel : INotifyPropertyChanged { Dictionary&lt;int, string&gt; itemList; KeyValuePair&lt;int, string&gt;? selectedItemList = null; public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String PropertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(PropertyName)); } } public Dictionary&lt;int, string&gt; ItemList { get { itemList = GetItemsList(); return itemList; } } private Dictionary&lt;int, string&gt; GetItemsList() { var resultList = new Dictionary&lt;int, string&gt;(); resultList.Add(1, "Item I"); resultList.Add(2, "Item II"); resultList.Add(3, "Item III"); return resultList; } public KeyValuePair&lt;int, string&gt;? SelectedItemList { get { return selectedItemList; } set { selectedItemList = value; NotifyPropertyChanged("SelectedNewPtLevel"); } } } </code></pre> <p>FirstPage.xaml</p> <pre><code>&lt;UserControl.DataContext&gt; &lt;vm:MainViewModel /&gt; &lt;/UserControl.DataContext&gt; &lt;Grid&gt; &lt;ListView Name="lstFirst" HorizontalAlignment="Left" Height="auto" ItemsSource="{Binding Path=ItemList}" SelectedItem="{Binding Path=SelectedItemList}" FontSize="11" SelectionMode="Single" Width="310"&gt; &lt;ListView.ItemContainerStyle&gt; &lt;Style TargetType="ListViewItem"&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="IsSelected" Value="True"&gt; &lt;Setter Property="Background" Value="Yellow"&gt;&lt;/Setter&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/ListView.ItemContainerStyle&gt; &lt;ListView.View&gt; &lt;GridView&gt; &lt;GridView.ColumnHeaderContainerStyle&gt; &lt;Style&gt; &lt;Setter Property="FrameworkElement.Visibility" Value="Collapsed"/&gt; &lt;/Style&gt; &lt;/GridView.ColumnHeaderContainerStyle&gt; &lt;GridViewColumn Width="69" DisplayMemberBinding="{Binding Key}"&gt;&lt;/GridViewColumn&gt; &lt;GridViewColumn Width="109" DisplayMemberBinding="{Binding Value}"&gt;&lt;/GridViewColumn&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; &lt;/Grid&gt; </code></pre> <p>SecondPage.xaml</p> <pre><code>&lt;UserControl.DataContext&gt; &lt;vm:MainViewModel /&gt; &lt;/UserControl.DataContext&gt; &lt;Grid&gt; &lt;ListView Name="lstSecond" HorizontalAlignment="Left" Height="auto" ItemsSource="{Binding Path=ItemList}" SelectedItem="{Binding Path=SelectedItemList}" FontSize="11" SelectionMode="Single" Width="310"&gt; &lt;ListView.ItemContainerStyle&gt; &lt;Style TargetType="ListViewItem"&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="IsSelected" Value="True"&gt; &lt;Setter Property="Background" Value="Pink"&gt;&lt;/Setter&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/ListView.ItemContainerStyle&gt; &lt;ListView.View&gt; &lt;GridView&gt; &lt;GridView.ColumnHeaderContainerStyle&gt; &lt;Style&gt; &lt;Setter Property="FrameworkElement.Visibility" Value="Collapsed"/&gt; &lt;/Style&gt; &lt;/GridView.ColumnHeaderContainerStyle&gt; &lt;GridViewColumn Width="69" DisplayMemberBinding="{Binding Key}"&gt;&lt;/GridViewColumn&gt; &lt;GridViewColumn Width="109" DisplayMemberBinding="{Binding Value}"&gt;&lt;/GridViewColumn&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; &lt;/Grid&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload