【问题标题】:How do I populate XAML Listbox Items from a method that requires a single argument?如何从需要单个参数的方法填充 XAML 列表框项?
【发布时间】:2010-07-12 12:06:36
【问题描述】:

所以我有一个列表框:

<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 中的当前地址列表。要生成这样的地址列表,只需调用接受当前邮政编码作为参数的方法,但我不知道如何调用需要来自静态数据源的参数的方法。

谁能帮忙?

【问题讨论】:

    标签: c# xaml datasource


    【解决方案1】:

    使用 Collection 属性和 PostCode 属性创建一个视图。绑定 PostCode TwoWay,让 setter 为 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>
    

    注意:我的“地址”类可能与你的不同。

    【讨论】:

    • 我一定会试试这个。谢谢!
    • 具有讽刺意味的是,我无法投票,因为我没有帐户。但如果它得到回报,你肯定会得到绿色的大勾号!
    • 我们已决定重新调整我项目的这一部分...但没有更简单的建议...
    猜你喜欢
    • 1970-01-01
    • 2011-05-18
    • 2010-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多