Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8792585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:59:57+00:00 2026-06-13T22:59:57+00:00

I’m having an issue where on my listview, when I select an item, the

  • 0

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?

MainWindow.xaml

    <UserControl.DataContext>
        <vm:MainViewModel />
    </UserControl.DataContext>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"></ColumnDefinition>
            <ColumnDefinition Width="5"></ColumnDefinition>
            <ColumnDefinition Width="auto"></ColumnDefinition>
            <ColumnDefinition Width="5"></ColumnDefinition>
            <ColumnDefinition Width="auto"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Button Name="btnPrevious" Content="&lt;" Grid.Column="0" Height="300" Click="btnPrevious_Click"></Button>
        <Grid Width="310" Height="300" Grid.Column="2">
            <Frame x:Name="UserControlContainer" NavigationUIVisibility="Hidden" Width="310" />
        </Grid>
        <Button Name="btnNext" Content="&gt;" Grid.Column="4" Height="300" Click="btnNext_Click"></Button>
    </Grid>

MainWindow.xaml.cs

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

MainViewModel.cs

public class MainViewModel : INotifyPropertyChanged
{
    Dictionary<int, string> itemList;
    KeyValuePair<int, string>? selectedItemList = null;

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String PropertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
        }
    }

    public Dictionary<int, string> ItemList
    {
        get
        {
            itemList = GetItemsList();
            return itemList;
        }
    }

    private Dictionary<int, string> GetItemsList()
    {
        var resultList = new Dictionary<int, string>();

        resultList.Add(1, "Item I");
        resultList.Add(2, "Item II");
        resultList.Add(3, "Item III");

        return resultList;
    }

    public KeyValuePair<int, string>? SelectedItemList
    {
        get 
        { 
            return selectedItemList; 
        }
        set
        {
            selectedItemList = value;
            NotifyPropertyChanged("SelectedNewPtLevel");
        }
    }
}

FirstPage.xaml

<UserControl.DataContext>
        <vm:MainViewModel />
    </UserControl.DataContext>

    <Grid>
        <ListView Name="lstFirst" HorizontalAlignment="Left"  
                    Height="auto" ItemsSource="{Binding Path=ItemList}" SelectedItem="{Binding Path=SelectedItemList}" 
                    FontSize="11" SelectionMode="Single" Width="310">
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="Yellow"></Setter>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.View>
                <GridView>
                    <GridView.ColumnHeaderContainerStyle>
                        <Style>
                            <Setter Property="FrameworkElement.Visibility" Value="Collapsed"/>
                        </Style>
                    </GridView.ColumnHeaderContainerStyle>
                    <GridViewColumn Width="69" DisplayMemberBinding="{Binding Key}"></GridViewColumn>
                    <GridViewColumn Width="109" DisplayMemberBinding="{Binding Value}"></GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>

SecondPage.xaml

<UserControl.DataContext>
        <vm:MainViewModel />
    </UserControl.DataContext>

    <Grid>
        <ListView Name="lstSecond" HorizontalAlignment="Left"
                    Height="auto" ItemsSource="{Binding Path=ItemList}" SelectedItem="{Binding Path=SelectedItemList}" 
                    FontSize="11" SelectionMode="Single" Width="310">
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="Pink"></Setter>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.View>
                <GridView>
                    <GridView.ColumnHeaderContainerStyle>
                        <Style>
                            <Setter Property="FrameworkElement.Visibility" Value="Collapsed"/>
                        </Style>
                    </GridView.ColumnHeaderContainerStyle>
                    <GridViewColumn Width="69" DisplayMemberBinding="{Binding Key}"></GridViewColumn>
                    <GridViewColumn Width="109" DisplayMemberBinding="{Binding Value}"></GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:59:58+00:00Added an answer on June 13, 2026 at 10:59 pm

    The problem is that, when you click to the second page, a new instance of the View Model class is getting instantiated. To get around this I would suggest binding the data context to a “Locator” class.

    <UserControl DataContext="{Binding Source={StaticResource Locator},Path=Main}"
        ... />
    

    (Here the Locator instance sits in the main Resources section of your application, as <vm:Locator x:Key=Locator />.)

    The Locator class keeps track of a single instance of your model.

    public class Locator
    {
        private MainViewModel _model;
    
        public MainViewModel Main
        {
            get
            {
                if (_model == null) _model = new MainViewModel();
                return _model;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
This could be a duplicate question, but I have no idea what search terms
I'm trying to select an H1 element which is the second-child in its group
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.