WPF - 显示下一页时属性为null

时间:2012-11-07 21:20:10

标签: c# wpf listview

我有一个问题,在我的listview上,当我选择一个项目时,行突出显示和属性的值确实设置了。但是当我点击按钮显示第二页时,列表视图没有突出显示/从前一个中选择。该属性为null。每个页面上的两个列表视图引用了itemsource和selecteditem的相同属性。任何人都可以帮我解释为什么第二页不会引发财产?

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 个答案:

答案 0 :(得分:2)

问题是,当您单击第二页时,将实例化View Model类的新实例。为了解决这个问题,我建议将数据上下文绑定到“Locator”类。

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

(此处Locator实例位于应用程序的主要资源部分,<vm:Locator x:Key=Locator />。)

Locator类会跟踪模型的单个实例。

public class Locator
{
    private MainViewModel _model;

    public MainViewModel Main
    {
        get
        {
            if (_model == null) _model = new MainViewModel();
            return _model;
        }
    }
}