C# 如何从需要单个参数的方法填充XAML列表框项?
所以我有一个列表框:C# 如何从需要单个参数的方法填充XAML列表框项?,c#,xaml,datasource,C#,Xaml,Datasource,所以我有一个列表框: <ListBox Grid.Row="0" Grid.Column="1" Grid.RowSpan="2"> //<----Item's Data Source Method Call Here? <ListBox.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel Orientation=
<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>
/使用集合属性和邮政编码属性创建视图。双向绑定邮政编码,让setter为Collection属性引发OnPropertyChanged,并让Collection属性基于邮政编码属性的当前值返回集合
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>
内部类MyView:INotifyPropertyChanged{
私人字符串(邮政编码);;
公共字符串邮政编码{
获取{return\u postCode;}
设置{
_邮政编码=价值;
OnProperty变更(“邮政编码”);
OnPropertyChanged(“FilteredItems”);
}
}
公共ObservableCollection项{get;set;}
公共IEnumerable FilteredItems{
获取{return Items.Where(o=>o.PostCode==\u PostCode.ToArray();}
}
公共事件属性更改事件处理程序属性更改;
私有void OnPropertyChanged(字符串propertyName){
if(PropertyChanged!=null){
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
注意:我的“地址”类可能与您的不同。创建一个包含集合属性和邮政编码属性的视图。双向绑定邮政编码,让setter为Collection属性引发OnPropertyChanged,并让Collection属性基于邮政编码属性的当前值返回集合
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>
内部类MyView:INotifyPropertyChanged{
私人字符串(邮政编码);;
公共字符串邮政编码{
获取{return\u postCode;}
设置{
_邮政编码=价值;
OnProperty变更(“邮政编码”);
OnPropertyChanged(“FilteredItems”);
}
}
公共ObservableCollection项{get;set;}
公共IEnumerable FilteredItems{
获取{return Items.Where(o=>o.PostCode==\u PostCode.ToArray();}
}
公共事件属性更改事件处理程序属性更改;
私有void OnPropertyChanged(字符串propertyName){
if(PropertyChanged!=null){
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
注意:我的“Address”类可能与你的不同。我一定会试试这个。谢谢具有讽刺意味的是,我无法投票,因为我没有账户。但是,如果它有回报的话,你肯定会得到巨大的绿色回报!我们决定重新审视我项目的这一部分。。。但是在没有更简单的建议的情况下…我一定会试试这个。谢谢具有讽刺意味的是,我无法投票,因为我没有账户。但是,如果它有回报的话,你肯定会得到巨大的绿色回报!我们决定重新审视我项目的这一部分。。。但是在没有更简单的建议的情况下。。。