WPF用户控件内的绑定

为了进入WPF世界并习惯绑定,我创建了一个用户控件来定义搜索filter。 根据所需的filter,用户可以输入文本,选择日期或选择combobox中的项目。 这是一个包含已创建搜索控件的三个实例的示例,每个实例的类型不同:

搜索过滤器示例

好消息是,一切正常,但我不确定是否一切都按预期完成。

SearchUserControl.xaml:

    

SearchUserControl.xaml.cs:

 using System; using System.Collections.Generic; using System.ComponentModel; using System.Windows; using Zefix.DataAccess; namespace Zefix.View.UserControls { ///  /// Interaction logic for SearchUserControl.xaml ///  public partial class SearchUserControl { #region Public Dependency Properties ///  /// The search value property ///  public static readonly DependencyProperty SearchValueProperty = DependencyProperty.Register("SearchValue", typeof (object), typeof (SearchUserControl)); ///  /// The available values property ///  public static readonly DependencyProperty AvailableValuesProperty = DependencyProperty.Register("AvailableValues", typeof (IEnumerable), typeof (SearchUserControl)); ///  /// The search type property ///  public static readonly DependencyProperty SearchTypeProperty = DependencyProperty.Register("SearchType", typeof (SearchType), typeof (SearchUserControl)); ///  /// The header text property ///  public static readonly DependencyProperty HeaderTextProperty = DependencyProperty.Register("HeaderText", typeof (string), typeof (SearchUserControl)); #endregion #region Private Dependency Properties ///  /// The combo box visiblity property ///  private static readonly DependencyProperty ComboBoxVisiblityProperty = DependencyProperty.Register("ComboBoxVisiblity", typeof (Visibility), typeof (SearchUserControl)); ///  /// The text box visiblity property ///  private static readonly DependencyProperty TextBoxVisiblityProperty = DependencyProperty.Register("TextBoxVisiblity", typeof (Visibility), typeof (SearchUserControl)); ///  /// The date picker visiblity property ///  private static readonly DependencyProperty DatePickerVisiblityProperty = DependencyProperty.Register("DatePickerVisiblity", typeof (Visibility), typeof (SearchUserControl)); #endregion #region Public Properties ///  /// Gets or sets the type of the search. ///  ///  /// The type of the search. ///  public SearchType SearchType { get { return (SearchType) GetValue(SearchTypeProperty); } set { SetValue(SearchTypeProperty, value); } } ///  /// Gets or sets the header text. ///  ///  /// The header text. ///  public string HeaderText { get { return (string) GetValue(HeaderTextProperty); } set { SetValue(HeaderTextProperty, value); } } ///  /// Gets or sets the available values. ///  ///  /// The available values. ///  public IEnumerable AvailableValues { get { return (IEnumerable) GetValue(AvailableValuesProperty); } set { SetValue(AvailableValuesProperty, value); } } ///  /// Gets or sets the search value. ///  ///  /// The search value. ///  public object SearchValue { get { return GetValue(SearchValueProperty); } set { SetValue(SearchValueProperty, value); } } #endregion #region Private Properties ///  /// Gets or sets the combo box visiblity. ///  ///  /// The combo box visiblity. ///  private Visibility ComboBoxVisiblity { get { return (Visibility) GetValue(ComboBoxVisiblityProperty); } set { SetValue(ComboBoxVisiblityProperty, value); } } ///  /// Gets or sets the date picker visiblity. ///  ///  /// The date picker visiblity. ///  private Visibility DatePickerVisiblity { get { return (Visibility) GetValue(DatePickerVisiblityProperty); } set { SetValue(DatePickerVisiblityProperty, value); } } ///  /// Gets or sets the text box visiblity. ///  ///  /// The text box visiblity. ///  private Visibility TextBoxVisiblity { get { return (Visibility) GetValue(TextBoxVisiblityProperty); } set { SetValue(TextBoxVisiblityProperty, value); } } #endregion #region Constructor ///  /// Initializes a new instance of the  class. ///  public SearchUserControl() { InitializeComponent(); DependencyPropertyDescriptor pd = DependencyPropertyDescriptor.FromProperty(SearchTypeProperty, typeof (SearchUserControl)); pd.AddValueChanged(this, OnSearchTypePropertyChanged); // Initialize default parameters SearchType = SearchType.Unknown; } #endregion #region Private Methods ///  /// Called when the search type property has changed. ///  /// The sender. /// The  instance containing the event data. private void OnSearchTypePropertyChanged(object sender, EventArgs e) { // Hide all editors DatePickerVisiblity = Visibility.Collapsed; ComboBoxVisiblity = Visibility.Collapsed; TextBoxVisiblity = Visibility.Collapsed; // Make the correct editor visible switch (SearchType) { case SearchType.Date: DatePickerVisiblity = Visibility.Visible; break; case SearchType.TextSelection: ComboBoxVisiblity = Visibility.Visible; break; case SearchType.Text: TextBoxVisiblity = Visibility.Visible; break; } } #endregion } } 

从父控件实例化搜索控件:

          

在SearchControl中,我想根据设置的SearchType显示正确的组件(文本框,日期选择器或combobox)。 为此,已创建xxxVisibility依赖项属性和属性(当SearchTypeProperty通知属性更改事件时,它们将被设置)。 由于没有理由将它们公开(它们仅在SearchControl中使用),我将它们设为私有; MSDN声明绑定属性必须是公共的。 该项目编译并运行没有问题,但是正在显示绑定的xxxVisibility属性的错误,并显示消息“Public member expected”(无法判断它是visual studio还是resharper告诉我)。

我在WPF概念方面创建此用户控件的方法是否正确? xxxVisibility属性应该是公共的(事件虽然我不想暴露它们)?

这是一个非常难以“回答”的问题,而不仅仅是“评论”。 在我个人看来,你的UserControl写得很好,据我所知,并没有违反任何规则。 虽然我没有看到声明private DependencyProperty有任何问题,但这很不寻常。 在这种情况下,开发人员通常选择使用private DependencyPropertyKey实现public Read Only DependencyProperty DependencyPropertyKey

 private static readonly DependencyPropertyKey ComboBoxVisiblityPropertyKey = DependencyProperty.RegisterReadOnly("ComboBoxVisiblity", typeof(int), typeof(SearchUserControl), new PropertyMetadata(Visibility.Collapsed)); public static readonly DependencyProperty ComboBoxVisiblityProperty = ComboBoxVisiblityPropertyKey.DependencyProperty; public int ComboBoxVisiblity { get { return (int)GetValue(ComboBoxVisiblityProperty); } protected set { SetValue(ComboBoxVisiblityPropertyKey, value); } } 

一些开发人员也可能认为您创建Visibility类型的属性而不是使用BoolToVisibilityConverter绑定bool值是不BoolToVisibilityConverter的,但是再次……这是您的特权。 总的来说,干得好! 🙂