WPFToolkit AutoComplete,为文本框和样式设置样式。下拉列表

时间:2013-10-11 12:55:17

标签: wpf styles wpftoolkit datatrigger autocompletebox

我有以下代码创建一个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" >

在VM中:

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是每个Model中可用的属性,它只返回类类型,即Company或Employee):

<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>

0 个答案:

没有答案