2014-06-07 5 views
1

MarketPrices로 Observable 컬렉션이 있습니다.이 관찰 가능한 컬렉션은 ItemsControl에 아래 바인딩되어 있습니다.조건부 ObservableCollection 바인딩

1) Observable Collection의 모든 항목을 표시하고 싶지 않고 Add를 클릭하고 Pair (GBPJPY, USDGBP ..)를 선택한 항목 만 항목 컨트롤에 표시해야합니다.

2) 사용자가 Comobobox의 항목을 GBPJPY에서 USDGBP로 변경 한 경우 GBPJPY의 가격 (DataTemplate)이 USDGBP를 업데이트해야합니다.

어떻게 두 조건을 모두 달성 할 수 있습니까? 아래 코드에는 실시간 업데이트가 없지만 프로젝트에는 relatime 가격 업데이트가 있으므로 가격 변동에 대한 관찰 가능한 업데이트가 있음을 유의하십시오.

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> 
+2

사용 A [CollectionViewSource] (http://msdn.microsoft.com/en-gb/ library/system.windows.data.collectionviewsource.aspx). 또한 [여기] (http://www.hanselman.com/blog/CollectionViewSourceIsCrazyUsefulForBindingToFilteredObservableCollectionsOnWindowsPhone8.aspx) 또는 [여기] (http://www.abhisheksur.com/2010/08/woring-with-icollectionviewsource)를 읽을 수도 있습니다. -에서.html) ... (또는 Google for it ...) – elgonzo

답변

0

선택된 쌍만 있지만 모든 쌍에는 해당하지 않습니까?

그런 경우 코드에 몇 가지 문제가 있습니다.

당신은 당신의 PriceModel 클래스에 사용 가능한 모든 쌍의 컬렉션을 필요로하지 않는

PriceModel. 또한, 당신은 당신이 당신의 PriceModel를 업데이트 할 수 있습니다, 당신의 의도는 한 쌍의 이름을 표시하는 것이었다 수 있으며,이 클래스에서 SelectedPair 속성을 필요가 없습니다

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를

Pairs 컬렉션을 보유하는 MarketPrices이라는 속성이 있지만 Selected 페어를 보유 할 속성은 없습니다. 유형이 PriceModelSelectedPair 속성을 추가합니다. 업데이트 된 코드는이를 좋아하는 것 :

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)); 
    } 
} 

MainWindow.xaml

그냥 텍스트 상자가 SelectedPair을 참조하기 위해 바인딩을 사용할 수 쌍 목록을 표시하고 업데이트하는 콤보를 사용할 수 있습니다.

업데이트 XAML은 다음과 같이 보일 것이다 :

<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 SelectedPair.AskPrice}" /> 
     </StackPanel> 
     <StackPanel Grid.Column="1" Orientation="Horizontal"> 
      <TextBlock Margin="2" Text="Offer Price" /> 
      <TextBlock Margin="2" Text="{Binding SelectedPair.OfferPrice}" /> 
     </StackPanel> 
    </Grid> 
</Grid> 

샘플 출력

enter image description here

관련 문제