簡體   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