ViewModel中的WPF绑定Listbox SelectedItem



我正在开发一个WPF应用程序。主页面由2列1行的网格组成。在第一列上,我有一个列表框,在第二列上,有一个名为"面板"的堆栈面板,我想在列表框的选定项目上更改它。我首先在视图(mainwindow.xaml.cs)中用列表框的selectionChanged事件实现了这一点,并成功了。我试图应用MVVM,所以我必须在viewModel中进行。主窗口的数据上下文是在其构造函数中设置的,并且是UserViewModel类型。userViewModel具有名为SelectedItem的ListBoxItem类型的属性,该属性在XAML中绑定到我的列表框的SelectedItem。无论何时更改,我都会在UserViewModel中使用"Parent",直到MainWindow,然后删除面板的所有子项并添加我想要的内容。EntriesUC是一个UserControl,它接受构造函数参数中的dataContext。它没有问题,因为当我在视图中实现SelectionChanged时它就工作了。问题是,每当我点击列表框中的任何项目时,都不会发生任何事情。

主窗口:

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

通常情况下,您不会将ListBoxItems添加到ListBox中,但您会直接添加字符串,并将字符串用作SelectedItem。ListBox会自动用ListBoxItem包装每个字符串。但如果你想这样做,你必须通过提取选定的字符串

message = selectedItem.Content.ToString();

最新更新