我有一个问题,在我的列表视图中,当我选择一个项目时,行突出显示并且属性的值确实被设置了。但是当我单击按钮显示第二页时,列表视图没有突出显示/从上一个页面中选择。该属性为空。每个页面上的两个列表视图引用 itemsource 和 selecteditem 的相同属性。谁能帮我解释为什么第二页不会触发属性?
主窗口.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="<" 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=">" Grid.Column="4" Height="300" Click="btnNext_Click"></Button>
</Grid>
主窗口.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;
}
}
主视图模型.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");
}
}
}
第一页.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>
第二页.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>