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?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.