在mainviewmodel上寻找MVVM文本块绑定的指导

Joh*_*ker 2 c# wpf mvvm

我有一个应用程序,我正在研究与股票有关.

它有点像Josh Smith的MVVM演示应用程序,有一些附加功能.

我有一个名为ShortQuote.cs的数据模型,它有一个viewmodel ShortQuoteViewModel,但现在没有使用ShortQuoteViewModel.

我有一个从XML数据文件ShortQuoteRepository创建一个类型的对象列表ShortQuote.在ShortQuoteRepository当利用点击在主窗口的左侧窗格中的命令列表会显示在标签.

我有一个组合框MainWindow,上面有一个自动收报机符号列表.当用户选择其中一个股票代码时,我想从ShortQuoteRepository中获取一个StockQuote对象(如果它存在于该股票代码中)并在MainWindow视图顶部的TextBlocks中显示它的内容.

我能让它工作的唯一方法是MainWindowViewModel在ShortQuote数据模型上公开属性镜像的"新"属性,然后一旦ShortQuote从ShortQuoteRepository 获取对象,我就将MainWindowViewModel's"新"属性设置为等于那些从检索到的对象.我将TextBlock绑定到MainWindowViewModel's"新"属性,它可以工作.

我觉得这是一个"黑客",有一个"更好"的方法来实现这一点,而不必在MainWindowViewModel上创建"新"属性,并请求一些指导和建议如何在更多只需XAML或XAML与MainWindowViewModel不需要创建这些"新"属性的代码的组合即可直接使用.

谁能帮我?

Rob*_*cus 5

还可以使ShortQuote成为ViewModel(或者至少使用ObservableObject,如果您使用的是MVVM Light).然后,您可以绑定SelectedItemShortQuoteviewmodel上的属性(将其标记为TwoWay).然后您的View可以SelectedItem根据需要引用它.

将其添加到ShortQuoteViewModel.cs

private ShortQuote _selectedQuote;

public ShortQuote SelectedQuote
{    
     get { return _selectedQuote; }
     set
     {
          if(value != _selectedQuote)
          {
               _selectedQuote = value;
               RaisePropertyChanged("SelectedQuote");
          }
      }
}
Run Code Online (Sandbox Code Playgroud)

XAML:

<ListBox ItemsSource="{Binding Quotes}" SelectedItem={Binding SelectedQuote, Mode=TwoWay}"/>

<TextBlock Text="{Binding SelectedQuote.Ticker}"/>
Run Code Online (Sandbox Code Playgroud)

你还必须改变你的ShortQuote类(可能是ShortQuote.cs)来实现INotifyPropertyChanged,或者通过显式实现INPC,或者通过继承自ViewModel或者ObservableObject(如果使用MVVM Light).你没有指定,但它很受欢迎,如果你不是使用它,你应该考虑这样做).

编辑这是一个工作样本:

XAML:

<Window x:Class="StockQuoteExample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="24"/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <TextBlock>
            <Run Text="Company Name: "/>
            <Run Text="{Binding SelectedTicker.Ticker}"/>
            <Run Text="  Symbol:  "/>
            <Run Text="{Binding SelectedTicker.StockName}"/>
            <Run Text="  Tick Price:  "/>
            <Run Text="{Binding SelectedTicker.TickPrice}"/>
        </TextBlock>

        <ListBox Grid.Row="1" Margin="10" ItemsSource="{Binding StockQuotes}" SelectedItem="{Binding SelectedTicker, Mode=TwoWay}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Width="120" Text="{Binding Ticker}"/>
                        <TextBlock Width="120" Margin="5,0,5,0" Text="{Binding StockName}"/>
                        <TextBlock Width="120" Text="{Binding TickPrice}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

代码隐藏:

using System.Windows;
using StockQuoteExample.ViewModel;

namespace StockQuoteExample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.DataContext = new StockQuoteViewModel();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

视图模型:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using StockQuoteExample.DataModel;

namespace StockQuoteExample.ViewModel
{
    class StockQuoteViewModel : INotifyPropertyChanged
    {

        public ObservableCollection<StockQuote> StockQuotes { get; set; }

        private StockQuote _selectedTicker;

        public StockQuote SelectedTicker
        {
            get { return _selectedTicker; }
            set
            {
                if (value != _selectedTicker)
                {
                    _selectedTicker = value;
                    OnPropertyChanged("SelectedTicker");
                }
            }
        }


        public StockQuoteViewModel()
        {
            StockQuotes = new ObservableCollection<StockQuote>()
                              {
                                  new StockQuote() {StockName = "Microsoft", TickPrice = 150m, Ticker = "MSFT"},
                                  new StockQuote() {StockName = "Apple", TickPrice = 600m, Ticker = "AAPL"}
                              };
        }




        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

    }
}
Run Code Online (Sandbox Code Playgroud)

数据模型:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace StockQuoteExample.DataModel
{
    public class StockQuote : INotifyPropertyChanged
    {

        private string _ticker;

        public string Ticker
        {
            get { return _ticker; }
            set
            {
                if (value != _ticker)
                {
                    _ticker = value;
                    OnPropertyChanged("Ticker");
                }
            }
        }

        private string _stockName;

        public string StockName
        {
            get { return _stockName; }
            set
            {
                if (value != _stockName)
                {
                    _stockName = value;
                    OnPropertyChanged("StockName");
                }
            }
        }

        private decimal _tickPrice;

        public decimal TickPrice
        {
            get { return _tickPrice; }
            set
            {
                if (value != _tickPrice)
                {
                    _tickPrice = value;
                    OnPropertyChanged("TickPrice");
                }
            }
        }




        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
Run Code Online (Sandbox Code Playgroud)