我有以下代码创建一个 AutoCompleteBox(使用 WPFToolkit),它将公司和员工组合成一个搜索功能。我被迫使用 ObservableCollection,因为 AutoCompleteBox 不知道如何处理 CompositeCollections(至少根据我在该主题上可以找到的内容)。
我想设置下拉菜单和文本框的样式,使公司的文本为红色,员工的文本为绿色。我可以为整个下拉列表设置文本颜色,如下面的 ResourceDictionary 所示,但找不到使用 DataTrigger 的方法(我下面的内容不起作用)。
我不确定如何将 DataTrigger 绑定到 SearchCollection 中每个对象的 ModelType 属性。
编辑:
再次查看代码后,我意识到 VM 中没有公开一个名为 ModelType 的属性,ModelType 属性位于 SearchCollection 中的每个对象上。如何绑定到集合中每个对象的 ModelType?
在 XAML 中:
<toolkit:AutoCompleteBox Name="OmniSearchTextBox"
ItemsSource="{Binding SearchCollection}"
SelectedItem="{Binding Selection, Mode=TwoWay, UpdateSourceTrigger=Explicit}"
KeyUp="OmniSearch_KeyUp"
MouseLeftButtonUp="OmniSearch_MouseLeftButtonUp"
IsTextCompletionEnabled="False"
FilterMode="Contains"
VerticalAlignment="Top" Margin="10,0" >
在虚拟机中:
public sealed class SearchViewModel : ViewModelBase
{
private ViewModelLocator _vmLocator;
private ObservableCollection<Company> _companyList;
private ObservableCollection<Employee> _employeeList;
/// <summary>
/// Initializes a new instance of the SearchViewModel class.
/// </summary>
public SearchViewModel()
{
try
{
_vmLocator = new ViewModelLocator();
_searchCompCollection.Add(companies);
_searchCompCollection.Add(employees);
foreach (Company co in CompanyList)
{
_searchCollection.Add(co);
}
foreach (Employee emp in EmployeeList)
{
_searchCollection.Add(emp);
}
}
catch (Exception ex)
{
}
}
private const string CompanyListPropertyName = "CompanyList";
public ObservableCollection<Company> CompanyList
{
get
{
return (_vmLocator.CompanyVM).CompanyList;
}
set
{
if (_companyList == value)
{
return;
}
_companyList = value;
RaisePropertyChanged(CompanyListPropertyName);
}
}
private const string EmployeeListPropertyName = "EmployeeList";
public ObservableCollection<Employee> EmployeeList
{
get
{
return (_vmLocator.EmployeeVM).EmployeeList;
}
set
{
if (_employeeList == value)
{
return;
}
_employeeList = value;
RaisePropertyChanged(EmployeeListPropertyName);
}
}
private ObservableCollection<ModelBase> _searchCollection = new ObservableCollection<ModelBase>();
public ObservableCollection<ModelBase> SearchCollection
{
get { return _searchCollection; }
}
private string _selection = null;
private string _origSelection = null;
public string Selection
{
get
{
return _selection;
}
set
{
if (_selection != value)
{
_origSelection = _selection;
_selection = value;
try
{
var item = _searchCollection.Single<Object>(x => x.ToString() == _selection);
this.SelectedObject = item;
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
}
private object _selectedObject = null;
private object _origSelectedObject = null;
public object SelectedObject
{
get {
return _selectedObject;
}
set
{
if (_selectedObject != value)
{
_origSelectedObject = _selectedObject;
_selectedObject = value;
switch(_selectedObject.GetType().BaseType.Name)
{
case "Company":
RaisePropertyChanged("SelectedObject",
(Company)_origSelectedObject,
(Company)_selectedObject,
true);
break;
case "Employee":
RaisePropertyChanged("SelectedObject",
(Employee)_origSelectedObject,
(Employee)_selectedObject,
true);
break;
default:
break;
}
RaisePropertyChanged("SelectedObject", _origSelectedObject, _selectedObject, true);
}
}
}
}
在 ResourceDictionary 中(ModelType 是每个模型中可用的属性,它只返回类类型,即公司或员工):
<Style TargetType="{x:Type toolkit:AutoCompleteBox}">
<Setter Property="Foreground" Value="DarkCyan" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=SearchCollection.ModelType}"
Value="Company">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=SearchCollection.ModelType}"
Value="Employee">
<Setter Property="Foreground" Value="Green" />
</DataTrigger>
</Style.Triggers>
</Style>