简体   繁体   English

WPF用户控件内的绑定

[英]Bindings inside WPF user control

In order to get into the WPF world and getting used to bindings, I've made a user control used to define a search filter. 为了进入WPF世界并习惯绑定,我创建了一个用户控件来定义搜索过滤器。 Depending on the wanted filter, the user can either enter a text, pick a date or select an item in a combo box. 根据所需的过滤器,用户可以输入文本,选择日期或选择组合框中的项目。 Here's an example with three instances of the created search control, each being of different type: 这是一个示例,其中包含三个创建的搜索控件实例,每个实例的类型不同:

搜索过滤器示例

The good news is, everything is working but I'm not sure if everything has been done as intended. 好消息是,一切正常,但我不确定是否一切都按预期完成。

SearchUserControl.xaml: SearchUserControl.xaml:

<UserControl x:Class="Zefix.View.UserControls.SearchUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="82" 
             d:DesignWidth="300" 
             Height="Auto"
             x:Name="SearchUserControlRoot">
    <Grid>
        <StackPanel>
            <Label Name="LabelHeaderText" Content="{Binding HeaderText, ElementName=SearchUserControlRoot}" />
            <TextBox Name="TextBoxSearchText" Text="{Binding SearchValue, ElementName=SearchUserControlRoot}" Visibility="{Binding TextBoxVisiblity, ElementName=SearchUserControlRoot}" />
            <DatePicker Name="DatePickerSearch" SelectedDate="{Binding SearchValue, ElementName=SearchUserControlRoot}" Visibility="{Binding DatePickerVisiblity, ElementName=SearchUserControlRoot}" />
            <ComboBox Name="ComboBoxSearch" Text="{Binding SearchValue, ElementName=SearchUserControlRoot}" ItemsSource="{Binding AvailableValues, ElementName=SearchUserControlRoot}" Visibility="{Binding ComboBoxVisiblity, ElementName=SearchUserControlRoot}" IsEditable="True" />
        </StackPanel>
    </Grid>
</UserControl>

SearchUserControl.xaml.cs: SearchUserControl.xaml.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using Zefix.DataAccess;

namespace Zefix.View.UserControls {
    /// <summary>
    ///     Interaction logic for SearchUserControl.xaml
    /// </summary>
    public partial class SearchUserControl {

        #region Public Dependency Properties

        /// <summary>
        /// The search value property
        /// </summary>
        public static readonly DependencyProperty SearchValueProperty =
            DependencyProperty.Register("SearchValue", typeof (object), typeof (SearchUserControl));

        /// <summary>
        /// The available values property
        /// </summary>
        public static readonly DependencyProperty AvailableValuesProperty =
            DependencyProperty.Register("AvailableValues", typeof (IEnumerable<object>), typeof (SearchUserControl));

        /// <summary>
        /// The search type property
        /// </summary>
        public static readonly DependencyProperty SearchTypeProperty =
            DependencyProperty.Register("SearchType", typeof (SearchType), typeof (SearchUserControl));

        /// <summary>
        /// The header text property
        /// </summary>
        public static readonly DependencyProperty HeaderTextProperty =
            DependencyProperty.Register("HeaderText", typeof (string), typeof (SearchUserControl));

        #endregion

        #region Private Dependency Properties

        /// <summary>
        /// The combo box visiblity property
        /// </summary>
        private static readonly DependencyProperty ComboBoxVisiblityProperty =
            DependencyProperty.Register("ComboBoxVisiblity", typeof (Visibility), typeof (SearchUserControl));

        /// <summary>
        /// The text box visiblity property
        /// </summary>
        private static readonly DependencyProperty TextBoxVisiblityProperty =
            DependencyProperty.Register("TextBoxVisiblity", typeof (Visibility), typeof (SearchUserControl));

        /// <summary>
        /// The date picker visiblity property
        /// </summary>
        private static readonly DependencyProperty DatePickerVisiblityProperty =
            DependencyProperty.Register("DatePickerVisiblity", typeof (Visibility), typeof (SearchUserControl));

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets or sets the type of the search.
        /// </summary>
        /// <value>
        ///     The type of the search.
        /// </value>
        public SearchType SearchType {
            get { return (SearchType) GetValue(SearchTypeProperty); }
            set { SetValue(SearchTypeProperty, value); }
        }

        /// <summary>
        ///     Gets or sets the header text.
        /// </summary>
        /// <value>
        ///     The header text.
        /// </value>
        public string HeaderText {
            get { return (string) GetValue(HeaderTextProperty); }
            set { SetValue(HeaderTextProperty, value); }
        }

        /// <summary>
        /// Gets or sets the available values.
        /// </summary>
        /// <value>
        /// The available values.
        /// </value>
        public IEnumerable<object> AvailableValues {
            get { return (IEnumerable<object>) GetValue(AvailableValuesProperty); }
            set { SetValue(AvailableValuesProperty, value); }
        }

        /// <summary>
        /// Gets or sets the search value.
        /// </summary>
        /// <value>
        /// The search value.
        /// </value>
        public object SearchValue {
            get { return GetValue(SearchValueProperty); }
            set { SetValue(SearchValueProperty, value); }
        }

        #endregion

        #region Private Properties

        /// <summary>
        /// Gets or sets the combo box visiblity.
        /// </summary>
        /// <value>
        /// The combo box visiblity.
        /// </value>
        private Visibility ComboBoxVisiblity {
            get { return (Visibility) GetValue(ComboBoxVisiblityProperty); }
            set { SetValue(ComboBoxVisiblityProperty, value); }
        }

        /// <summary>
        /// Gets or sets the date picker visiblity.
        /// </summary>
        /// <value>
        /// The date picker visiblity.
        /// </value>
        private Visibility DatePickerVisiblity {
            get { return (Visibility) GetValue(DatePickerVisiblityProperty); }
            set { SetValue(DatePickerVisiblityProperty, value); }
        }

        /// <summary>
        /// Gets or sets the text box visiblity.
        /// </summary>
        /// <value>
        /// The text box visiblity.
        /// </value>
        private Visibility TextBoxVisiblity {
            get { return (Visibility) GetValue(TextBoxVisiblityProperty); }
            set { SetValue(TextBoxVisiblityProperty, value); }
        }

        #endregion

        #region Constructor

        /// <summary>
        ///     Initializes a new instance of the <see cref="SearchUserControl" /> class.
        /// </summary>
        public SearchUserControl() {
            InitializeComponent();

            DependencyPropertyDescriptor pd = DependencyPropertyDescriptor.FromProperty(SearchTypeProperty, typeof (SearchUserControl));
            pd.AddValueChanged(this, OnSearchTypePropertyChanged);

            // Initialize default parameters
            SearchType = SearchType.Unknown;
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Called when the search type property has changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        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
    }
}

Instantiation of the search controls from the parent control: 从父控件实例化搜索控件:

        <ribbon:Tab Label="Search">
            <ribbon:Group Padding="0,5,0,5">
                <customcontrols:SearchUserControl x:Name="SearchUserControlCompanyName" HeaderText="company name" Margin="5,0,0,0" SearchType="Text" VerticalAlignment="Center" VerticalContentAlignment="Center" />
                <customcontrols:SearchUserControl x:Name="SearchUserControlCompanyNationality" HeaderText="company nationality (ISO3 code)" Margin="5,0,0,0" SearchType="TextSelection" AvailableValues="{Binding Path=CompaniesViewModel.ISO3Codes}" VerticalAlignment="Center" />
                <customcontrols:SearchUserControl x:Name="SearchUserControlDateFounded" HeaderText="date founded" Margin="5,0,0,0" SearchType="Date" VerticalAlignment="Center" VerticalContentAlignment="Center" />
                <ribbon:Button Context="StatusBarItem" Name="ButtonApplyFilter" Label="Search" ImageSourceSmall="/Resources/search_magnifying_glass_find.png" Margin="5,0,0,0" VerticalAlignment="Center" Click="OnButtonApplyFilterClicked" Command="{Binding Path=ApplyFilterCommand}" ScreenTipHeader="Apply the search filter" VerticalContentAlignment="Center" VariantSize="Large" />
            </ribbon:Group>
        </ribbon:Tab>

In the SearchControl I wanted to display the correct component (textbox, datepicker or combobox) according to the set SearchType. 在SearchControl中,我想根据设置的SearchType显示正确的组件(textbox,datepicker或combobox)。 For this the, xxxVisibility dependency properties and properties have been created (they are being set when the SearchTypeProperty notifies a property changed event). 为此,已创建xxxVisibility依赖项属性和属性(当SearchTypeProperty通知属性更改事件时,它们将被设置)。 As there is no reason to expose them as public (they are being used only inside the SearchControl), I've made them private; 因为没有理由将它们公开为公开(它们仅在SearchControl中使用),所以我将它们设为私有; MSDN states that bound properties MUST be public though. MSDN声明绑定属性必须是公共的。 The project compiles and runs without an issue, but errors are being shown for the bound xxxVisibility properties with the message 'Public member expected' (can't tell if it's visual studio or resharper telling me that). 该项目编译并运行没有问题,但是正在显示绑定的xxxVisibility属性的错误,并显示消息“Public member expected”(无法判断它是visual studio还是resharper告诉我)。

Is my approach to create this user control correct in respect to the WPF concepts? 我在WPF概念方面创建此用户控件的方法是否正确? Should the xxxVisibility properties be public (event though I don't want to expose them)? xxxVisibility属性应该是公共的(事件虽然我不想暴露它们)?

This is a very difficult question to 'answer', rather than just 'comment' on. 这是一个非常难以回答的问题,而不仅仅是“评论”。 In my personal opinion, your UserControl has been written well and as far as I can see doesn't break any rules. 在我个人看来,你的UserControl写得很好,据我所知,并没有违反任何规则。 Although I don't see any problem with declaring a private DependencyProperty , it is unusual. 虽然我没有看到声明private DependencyProperty任何问题,但这不寻常的。 In this situation, developers often chose to implement a public Read Only DependencyProperty with a private DependencyPropertyKey instead: 在这种情况下,开发人员通常选择使用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); }
}

Some developers may also think it unusual that you are creating properties of type Visibility rather than binding bool values with BoolToVisibilityConverter s, but again... that is your prerogative. 一些开发人员也可能认为创建Visibility类型的属性而不是使用BoolToVisibilityConverter绑定bool值是不BoolToVisibilityConverter的,但是再次......这是你的特权。 Overall, well done! 总的来说,干得好! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM