我有一个List
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}"/>
我尝试使用转换器执行此操作,但是转换器仅允许一个参数。我不想在转换器中分离参数,因为它很丑陋。知道如何在不使用转换器中分离参数的情况下执行此操作吗?
答案 0 :(得分:1)
这就是我要做的。我将根据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中应用过滤器