views:

51

answers:

1

So I have a Listbox:

<ListBox Grid.Row="0" Grid.Column="1" Grid.RowSpan="2"> //<----Item's Data Source Method Call Here?
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel
             Orientation="Horizontal"
             IsItemsHost="true"  />
        </ItemsPanelTemplate>
     </ListBox.ItemsPanel>
     <ListBox.ItemTemplate>
       <DataTemplate>
          <StackPanel Grid.Row="0" Grid.Column="1" Grid.RowSpan="2" Orientation="Vertical">
             <Label Content="{Binding Address1}"></Label>
             <Label Content="{Binding Address2}"></Label>
             <Label Content="{Binding Town}"></Label>
             <Label Content="{Binding Postcode}"></Label>
             <Label Content="{Binding Country}"></Label>
             <CheckBox Content="{Binding Include}"></CheckBox>
          </StackPanel>
       </DataTemplate>
     </ListBox.ItemTemplate>
</ListBox>

What I would like to do is set the item's data source to a list of addresses all of which have the same postal code. So I need a list of the current addresses in an Observable Collection with the same postcode. To generate such a list of addresses would only take a call to a method which accepts the current postcode as a an argument but I don't know how to call a method that requires an argument from a static datasource.

Can anyone help?

+1  A: 

Create a view with a Collection property and a PostCode property. Bind the PostCode TwoWay, let the setter raise OnPropertyChanged for the Collection property, and let the collection property return a collection based on the current value of the PostCode property.

internal class MyView : INotifyPropertyChanged {
   private string _postCode;

   public string PostCode {
      get { return _postCode; }
      set {
         _postCode = value;
         OnPropertyChanged("PostCode");
         OnPropertyChanged("FilteredItems");
      }
   }

   public ObservableCollection<Address> Items { get; set; }

   public IEnumerable<Address> FilteredItems {
      get { return Items.Where(o => o.PostCode == _postCode).ToArray(); }
   }

   public event PropertyChangedEventHandler PropertyChanged;

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

<Grid DataContext="{Binding Path=myViewInstance}">
   <TextBox Text="{Binding Path=PostCode, Mode=TwoWay}"/>
   <ListBox Grid.Row="0" Grid.Column="1" Grid.RowSpan="2" ItemsSource="{Binding Path=FilteredItems}">
      <ListBox.ItemsPanel>
         <ItemsPanelTemplate>
            <VirtualizingStackPanel
               Orientation="Horizontal"
               IsItemsHost="true"  />
         </ItemsPanelTemplate>
      </ListBox.ItemsPanel>
      <ListBox.ItemTemplate>
         <DataTemplate>
            <StackPanel Grid.Row="0" Grid.Column="1" Grid.RowSpan="2" Orientation="Vertical">
               <Label Content="{Binding Address1}"></Label>
               <Label Content="{Binding Address2}"></Label>
               <Label Content="{Binding Town}"></Label>
               <Label Content="{Binding PostCode}"></Label>
               <Label Content="{Binding Country}"></Label>
               <CheckBox Content="{Binding Include}"></CheckBox>
            </StackPanel>
         </DataTemplate>
      </ListBox.ItemTemplate>
   </ListBox>
</Grid>

Note: My "Address" class might differ from yours.

PjL
I will definitely try this. Thanks!
bert
No problem.. if it helps, feel free to vote me up ;-)
PjL
Ironically I cannot vote up as I do not have an account. But you will definitely be getting the big green tick if it pays off!
bert
We have decided to rescope this part of my project... but in the absence of a simpler suggestion...
bert