选择列表框中的标题时,在文本框中显示相应的内容。(MVVM WPF)

时间:2017-04-13 21:07:55

标签: c# wpf mvvm

我创建了一个数据库,我存储了本书的“标题”和“内容”。在程序中,标题存储在列表框中,当我填充列表时,我应该能够在单击列表中的“标题”时在文本框中显示相应的“内容”。我能够在Windows窗体应用程序中执行此操作而没有任何问题,但我正在尝试使用MVVM模式在WPF上实现此功能。我正在学习并希望得到你们的一些建议。以下是我到目前为止的情况。

XAML部分:

<Grid>
<ListBox x:Name="UserList" ItemsSource="{Binding UserEntry}" SelectedIndex="{Binding SelectedIndex}" HorizontalAlignment="Left" Height="323" Margin="18,16,0,0" VerticalAlignment="Top"
         Width="225" />
<TextBox x:Name="BookTextBox" Text="{Binding BookList, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="374" Margin="258,16,0,0" TextWrapping="Wrap"
         VerticalAlignment="Top" Width="292" VerticalScrollBarVisibility="Auto"  />
<Button x:Name="NewButton" Content="New" Command="{Binding NewCommand}" HorizontalAlignment="Left"
        Margin="18,344,0,0" VerticalAlignment="Top" Width="75" Height="30" />
<Button x:Name="List" Content="List" Command="{Binding ListCommand}" IsEnabled="{Binding CanClose}" HorizontalAlignment="Left"
        Margin="93,344,0,0" VerticalAlignment="Top" Width="75" Height="30" />
<Button x:Name="Delete" Content="Delete" HorizontalAlignment="Left" Margin="168,344,0,0"
        VerticalAlignment="Top" Width="75" Height="30" />

查看模型

private BookContext Context;
        private readonly ObservableCollection<string> _userEntry = new ObservableCollection<string>();
        private string _selectedIndex;
        private string _content;
        private bool mystate = true;
        public ICommand NewCommand
        {
            get { return new DelegateCommand(OpenNewWindow); }
        }

        public ICommand ListCommand
        {
            get
            {
                return new DelegateCommand(ListEntry);

            }
            set { ListCommand.CanExecute(mystate); }

        }

        public IEnumerable<string> UserEntry
        {
            get { return _userEntry; }
        }
        public void OpenNewWindow()
        {
            MainWindow mainwin = new MainWindow();
            mainwin.Hide();
            Create create = new Create();
            mainwin.Content = create;
            mainwin.Show();
        }

        public void ListEntry()
        {

            Context = new BookContext();
            var query = from b in Context.BookModels
                orderby b.BookId
                select b;
            foreach (var q in query)
            {
                _userEntry.Add(q.Title);
            }



        }

        public string BookList
        {
            get { return _content; }
            set { _content = value; }
        }

        public string SelectedIndex
        {
            get { return _selectedIndex; }
            set
            {
                if (_selectedIndex == value)
                {
                    var query = from b in Context.BookModels
                        where b.Title == _selectedIndex
                        select b;
                    foreach (var q in query)
                    {
                        _content = q.Content;
                    }
                }
            }
        }


    }

我尝试使用SelectIndex方式,但没有用。我可以填充列表框中的内容,但是当我选择列表框中的任何项目时,相应的内容不会出现在文本框中。如有任何更正或需要进一步的信息,请告诉我。

1 个答案:

答案 0 :(得分:2)

尝试以下代码。

 <StackPanel>
        <ListBox ItemsSource="{Binding Books}" x:Name="ListBoxBooks" DisplayMemberPath="Title"></ListBox>
        <TextBox Text="{Binding ElementName=ListBoxBooks,Path=SelectedItem.Content}"></TextBox>
    </StackPanel>

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new BookViewModel();
    }
}

public class BookViewModel
{
    public List<BookModel> Books { get; set; }

    public BookViewModel()
    {
        Books = new List<BookModel>()
        {
            new BookModel() {Title = "Title 1", Content = "Content 1"},
            new BookModel() {Title = "Title 2", Content = "Content 2"},
            new BookModel() {Title = "Title 3", Content = "Content 3"},
            new BookModel() {Title = "Title 4", Content = "Content 4"},
            new BookModel() {Title = "Title 5", Content = "Content 5"},
            new BookModel() {Title = "Title 6", Content = "Content 6"},
            new BookModel() {Title = "Title 7", Content = "Content 6"},
        };
    }
}

public class BookModel
{
    public string Title { get; set; }
    public string Content { get; set; }
}