[英]How do I populate XAML Listbox Items from a method that requires a single argument?
所以我有一个列表框:
<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>
我想做的是将商品的数据源设置为地址列表,所有地址都具有相同的邮政编码。 因此,我需要具有相同邮政编码的Observable集合中的当前地址的列表。 要生成这样的地址列表,只会调用对接受当前邮政编码作为参数的方法的调用,但是我不知道如何调用需要来自静态数据源的参数的方法。
有人可以帮忙吗?
创建一个具有Collection属性和PostCode属性的视图。 绑定PostCode TwoWay,让设置器为Collection属性提高OnPropertyChanged,并让collection属性根据PostCode属性的当前值返回一个集合。
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>
注意: 我的“地址”类可能与您的类不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.