问题
I'm working on a WPF application. The main page is made up of a grid with 2 column and one row. on the first column i have a listbox and on the 2nd column i have a stackpanel named thePanel which i want to change on selected item of a listbox.i first implemented this in the View(mainwindow.xaml.cs) with the selectionChanged event of the Listbox and it worked. I tried to apply the MVVM so i had to do it in the viewModel. the Data Context of the mainwindow is set in its constructor and is of Type UserViewModel. The userViewModel has a property of Type ListBoxItem named SelectedItem which is bound in XAML to the SelectedItem of my listbox. Whenever that is changed, I go up in the UserViewModel with "Parent" until MainWindow and then remove all the children of thePanel and add what I want. The EntriesUC is an UserControl that takes the dataContext in the constructor parameter. There is no problem with it since it worked when i implemented SelectionChanged in the View. The problem is whenever I click any item of the listbox, nothing happens.
Main window:
<Window x:Class="SyntheticLTM.View.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewNamespace="clr-namespace:SyntheticLTM.View"
Title="MainWindow" WindowState="Maximized" Height="350" Width="525">
<StackPanel>
//MENU IMPLEMENTATIOn
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="4*"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0">
<Button Content="{Binding Name}" Width="84" />
<ListBox Name="mainListBox" SelectionMode="Single" SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
<ListBoxItem>Data entries</ListBoxItem>
<ListBoxItem>Categories</ListBoxItem>
<ListBoxItem>Favorites</ListBoxItem>
<ListBoxItem>Search</ListBoxItem>
</ListBox>
</StackPanel>
<StackPanel Name="thePanel" Grid.Column="1" />
</Grid>
</StackPanel>
</Window>
UserViewModel:
private ListBoxItem selectedItem;
public ListBoxItem SelectedItem
{
get
{
return selectedItem;
}
set
{
selectedItem = value;
RaisePropertyChanged("SelectedItem");
var thePanel = new StackPanel();
thePanel=((((((selectedItem as ListBoxItem).Parent as ListBox).Parent as StackPanel).Parent as Grid).Parent as StackPanel).Parent as MainWindow).thePanel;
string message;
message = selectedItem.ToString();
if (message == "Data entries")
{
var allEntries = new CategoryViewModel();
foreach (var category in (thePanel.DataContext as UserViewModel).Categories)
allEntries.Entries = new ObservableCollection<EntryViewModel>(category.Entries);
thePanel.Children.Clear();
thePanel.Children.Add(new EntriesUC(allEntries));
}
// implementation for all the other list items...
}
}
回答1:
Normally you wouldn't add ListBoxItems to the ListBox but you would directly add the strings and would work with a string as SelectedItem. The ListBox would wrap each string with a ListBoxItem automatically. But if you want to do it this way, you have to extract the selected string by
message = selectedItem.Content.ToString();
来源:https://stackoverflow.com/questions/26480975/wpf-binding-listbox-selecteditem-in-viewmodel