简体   繁体   English

如何从需要单个参数的方法填充XAML列表框项目?

[英]How do I populate XAML Listbox Items from a method that requires a single argument?

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. 因此,我需要具有相同邮政编码的Observable集合中的当前地址的列表。 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. 创建一个具有Collection属性和PostCode属性的视图。 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. 绑定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>

Note: My "Address" class might differ from yours. 注意: 我的“地址”类可能与您的类不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM