我有一个名为 SeiveList 的 ObservableCollection。我想要列表中的所有 SeiveIdSize(除了最后一个,因为它没有用)并为 Combobox 设置 DataContext。我添加了
seiveCmb.DataContext = GlobalUtils.SeiveList;
seiveCmb.DisplayMemberPath = // WHAT SHOULD GO HERE. hOW TO ONLY SHOW SeiveIdSize
// XML
<ComboBox Name="seiveCmb" ItemsSource="{Binding}" Grid.Column="1" Grid.Row="1" Margin="2" SelectedIndex="0" ></ComboBox>
根据塞巴斯蒂安的建议编辑:目前,我刚刚尝试了组合框的列表。我的Seive类:
public class Seive : INotifyPropertyChanged
{
// Other Members
private bool isSelected;
public bool IsSelected
{
get { return isSelected; }
set
{
isSelected = value;
OnPropertyChanged("IsSelected");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string p)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(p));
}
}
在我的 Window .xaml 文件中:
<Window.Resources>
<CollectionViewSource Source="{Binding Path=comboSeives}"
x:Key="comboSeivesFiltered"
Filter="ComboSeiveFilter">
</CollectionViewSource>
</Window.Resources>
<ComboBox Name="seiveCmb" ItemsSource="{Binding Source={StaticResource comboSeivesFiltered}}" DisplayMemberPath="SeiveIdSize"
Grid.Column="1" Grid.Row="1" Margin="2" SelectedIndex="0"
></ComboBox>
在 Window .cs 文件中:
public ObservableCollection<Seive> comboSeives { get; set; }
// Initial original data in Window_Loaded method
comboSeives = GlobalUtils.SeiveList;
public void ComboSeiveFilter(object sender, FilterEventArgs e)
{
Seive sv = e.Item as Seive;
// Add items those is != "TOTAL" and isSelected == false
if (sv.SeiveIdSize != "TOTAL" && sv.IsSelected == false)
e.Accepted = true;
else
e.Accepted = false;
}
如果 id 为“TOTAL”或 isSelected 为 false(即未添加到网格中),则仅返回 true 并将其添加到其中。初始所有记录都有 isSelected = false。
这是我从您对本网站的解释和帮助中了解到的。并实现了这一点。但是在运行时,我在组合框中看不到任何东西。我试图调试在过滤器方法添加中断,但它从未到达那里。你能从上面的代码中指出我在哪里犯了错误。
感谢任何帮助。
谢谢