繁体   English   中英

WPF绑定ViewModel中的Listbox SelectedItem

[英]WPF binding Listbox SelectedItem in ViewModel

我正在处理WPF应用程序。 主页由2列和1行的网格组成。 在第一列上,我有一个列表框,在第二列上,我有一个名为thePanel的堆栈面板,我想在列表框的选定项上进行更改。我首先在View(mainwindow.xaml.cs)中使用selectionChanged事件实现了此功能列表框,它的工作原理。 我尝试应用MVVM,因此必须在viewModel中进行操作。 主窗口的数据上下文在其构造函数中设置,并且类型为UserViewModel。 userViewModel具有一个名为SelectedItem的ListBoxItem类型的属性,该属性在XAML中绑定到我的列表框的SelectedItem。 每当更改时,我都会在UserViewModel中使用“父项”,直到MainWindow,然后删除thePanel的所有子项并添加我想要的内容。 EntriesUC是一个UserControl,它在构造函数参数中采用dataContext。 它没有问题,因为它在我在View中实现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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM