条件上的ObservableCollection绑定

时间:2014-06-07 10:57:43

标签: c# wpf silverlight xaml

我有带MarketPrices的Observable集合,这个可观察的集合我已经将ItemsControl绑定到下面。

1)现在我不想显示Observable Collection中的所有项目,只想显示用户点击添加和选择的对(GBPJPY,USDGBP ..)需要在Items Control中显示的项目。

2)如果用户将Comobobox中的项目从GBPJPY更改为USDGBP,那么GBPJPY的价格(DataTemplate)需要更新USDGBP。

如何实现这两个条件。请注意,下面的代码没有实时更新,但在项目中我也有续订价格更新,所以可以观察到价格变化的收集更新。

enter image description here 代码到目前为止

 public class PriceModel : INotifyPropertyChanged
    {
        private double _askPrice;
        private double _offerPrice;
        private string _selectedPair;

        public PriceModel()
        {
            Pairs = new ObservableCollection<string> {"GBPUSD", "GBPEUR", "USDGBP", "GBPJPY"};
        }

        public double AskPrice
        {
            get { return _askPrice; }
            set
            {
                _askPrice = value;
                OnPropertyChanged("AskPrice");
            }
        }

        public double OfferPrice
        {
            get { return _offerPrice; }
            set
            {
                _offerPrice = value;
                OnPropertyChanged("OfferPrice");
            }
        }

        public string SelectedPair
        {
            get { return _selectedPair; }
            set
            {
                _selectedPair = value;
                OnPropertyChanged(SelectedPair);
            }
        }

        public ObservableCollection<string> Pairs { get; set; }
        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }



   public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;

            MarketPrices = new ObservableCollection<PriceModel>
            {
                new PriceModel {AskPrice = 1.60345, OfferPrice = 1.60335, SelectedPair = "GBPUSD"},
                new PriceModel {AskPrice = 1.71345, OfferPrice = 1.71335, SelectedPair = "GBPEUR"},
                new PriceModel {AskPrice = 1.23345, OfferPrice = 1.23335, SelectedPair = "USDGBP"},
                new PriceModel {AskPrice = 1.34345, OfferPrice = 1.34335, SelectedPair = "GBPJPY"}
            };
        }

        public ObservableCollection<PriceModel> MarketPrices { get; set; } 
    }

XAML

 <ScrollViewer VerticalScrollBarVisibility="Auto">
        <ItemsControl ItemsSource="{Binding MarketPrices}">
            <ItemsControl.ItemContainerStyle>
                <Style>
                    <Setter Property="FrameworkElement.Margin" Value="5" />
                </Style>
            </ItemsControl.ItemContainerStyle>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <WrapPanel AllowDrop="True" ClipToBounds="True">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition />
                                <RowDefinition />
                            </Grid.RowDefinitions>
                            <ComboBox ItemsSource="{Binding Pairs}" SelectedItem="{Binding SelectedPair}" />
                            <Grid Grid.Row="1">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition />
                                    <ColumnDefinition />
                                </Grid.ColumnDefinitions>
                                <StackPanel Grid.Column="0" Orientation="Horizontal">
                                    <TextBlock Margin="2" Text="Ask Price" />
                                    <TextBlock Margin="2" Text="{Binding AskPrice}" />
                                </StackPanel>
                                <StackPanel Grid.Column="1" Orientation="Horizontal">
                                    <TextBlock Margin="2" Text="Offer Price" />
                                    <TextBlock Margin="2" Text="{Binding OfferPrice}" />
                                </StackPanel>
                            </Grid>
                        </Grid>
                    </WrapPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </ScrollViewer>

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题,你想在ComboBox中显示一对配对列表,只显示一对选择对的详细信息,但不是所有配对的详细信息?

如果是这种情况,代码就会出现问题。

<强> PriceModel

您不需要PriceModel类中所有可用对的集合。此外,您不需要此类中的SelectedPair属性,可能您的意图是指示该对的名称,您可以将您的PriceModel更新为:

public class PriceModel : INotifyPropertyChanged
{
    private double _askPrice;
    private double _offerPrice;
    private string _pair;

    public PriceModel(string pair)
    {
        _pair = pair;
    }

    public string Pair
    {
        get { return _pair; }
    }

    public double AskPrice
    {
        get { return _askPrice; }
        set
        {
            _askPrice = value;
            OnPropertyChanged("AskPrice");
        }
    }

    public double OfferPrice
    {
        get { return _offerPrice; }
        set
        {
            _offerPrice = value;
            OnPropertyChanged("OfferPrice");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

<强> MainWindow.xaml.cs

您有一个名为MarketPrices的属性来保存Pairs集合,但没有属性来保存Selected对。添加名为SelectedPair的{​​{1}}类型的属性。更新后的代码是这样的:

PriceModel

<强> MainWindow.xaml

您可以使用ComboBox显示可用对的列表,并更新TextBoxes的绑定以引用SelectedPair。

更新XAML将如下所示:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private PriceModel _selectedPair;

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;

        MarketPrices = new ObservableCollection<PriceModel>
        {
            new PriceModel("GBPUSD") {AskPrice = 1.60345, OfferPrice = 1.60335, },
            new PriceModel("GBPEUR") {AskPrice = 1.71345, OfferPrice = 1.71335, },
            new PriceModel("USDGBP") {AskPrice = 1.23345, OfferPrice = 1.23335, },
            new PriceModel("GBPJPY") {AskPrice = 1.34345, OfferPrice = 1.34335, }
        };
    }

    public ObservableCollection<PriceModel> MarketPrices { get; set; }

    public PriceModel SelectedPair
    {
        get { return _selectedPair; }
        set
        {
            _selectedPair = value;
            OnPropertyChanged("SelectedPair");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

示例输出

enter image description here

相关问题