我有一个列表<SerialPort>其中项的IsOpen属性等于true或false。我只想显示IsOpen等于CheckBox.IsChecked属性的项。
ViewModel代码:
private ObservableCollection<SerialPort> _PortsCollection;
public ObservableCollection<SerialPort> PortsCollection
{
get { return _PortsCollection; }
set { _PortsCollection = value; OnPropertyChanged("PortsCollection"); }
}
private SerialPort _SelectedPort;
public SerialPort SelectedPort
{
get { return _SelectedPort; }
set { _SelectedPort = value; OnPropertyChanged("SelectedPort"); }
}
private bool _CheckBoxChecked;
public bool CheckBoxChecked
{
get { return _CheckBoxChecked; }
set { _CheckBoxChecked = value; OnPropertyChanged("CheckBoxChecked"); }
}
XAML:
<ComboBox
Width="100"
Height="23"
DisplayMemberPath="PortName"
ItemsSource="{Binding PortsCollection}"
SelectedItem="{Binding SelectedPort}"/>
<CheckBox
Content="Show ports where IsOpen == CheckBoxChecked"
IsChecked="{Binding CheckBoxChecked}"/>
我试过用转换器来做这个,但转换器只允许一个参数。我不想在转换器中分离参数,因为它很难看。知道如何在转换器中没有分离参数的情况下做到这一点吗?
以下是我的操作方法。我会根据CheckBoxChecked、的值更改PortsCollection返回的内容
private List<SerialPort> _AllPorts;
public ObservableCollection<SerialPort> PortsCollection
{
get
{
return new ObservableCollection<SerialPort>(_AllPorts.Where(x => x.IsOpen == CheckBoxChecked));
}
set { _PortsCollection = value; OnPropertyChanged("PortsCollection"); }
}
并在复选框更改时告诉GUI PortsCollection已更改。
private bool _CheckBoxChecked;
public bool CheckBoxChecked
{
get { return _CheckBoxChecked; }
set { _CheckBoxChecked = value;
OnPropertyChanged("CheckBoxChecked");
OnPropertyChanged("PortsCollection");
}
}
我认为没有办法在XAML 中应用过滤器