WPF -- 自定义控件 (UserControl)(二 下)

在 自定义控件 (UserControl)(二 上)中转载大神的一篇博文。通过一个UserControl例子主要介绍了User Control后台添加依赖项属性,路由事件以及命令的添加以及使用,这篇我主要介绍一下通过通过项目应用后的一些总结。
首先来分析一段前台代码:
     
                    
                        
                            
                                
                                    
                                
                            
                        
                        
                        
                            
                            
                        
                    
                 
                
 /// 
        /// 成员对象
        /// 
        public ObservableCollection MemberInfoData
        {
            get
            {
                return _MemberInfoData??(_MemberInfoData=new ObservableCollection());
            }

            set
            {
                _MemberInfoData = value; RaisePropertyChanged(() => MemberInfoData); RaisePropertyChanged(() => MemberCount);
                RefreshUser();
            }
        }
MemberInfoModel.cs
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Telewave.CompositeOperations.Model.BusinessModel.Base;using Telewave.CompositeOperations.Model.BusinessModel.UserInfo;namespace Telewave.CompositeOperations.Model.BusinessModel{ public class MemberInfoModel: HeadImgBaseModel { public MemberInfoModel(UserInfoModel model):base() { UserId = model.UserId; LoginId = model.LoginId; UserName = model.Name; HeadImageSource = model.HeadImg; OrgId = model.OrgId; OrgName = model.OrgName; Role = model.Roles; TelNumber = model.Mobile; switch(Role) { case "2": ShowRoles = "指挥员"; break; case "3": ShowRoles = "成员"; break; } } public MemberInfoModel() { } private string _UserId; /// /// 用户id(GUID) /// public string UserId { get { return _UserId; } set { _UserId = value;RaisePropertyChanged(() => UserId); } } private string _LoginId; /// /// 登陆ID(警号) /// public string LoginId { get { return _LoginId; } set { _LoginId = value; RaisePropertyChanged(() => LoginId); } } private string _Tips; /// /// /// public string Tips { get { return _Tips; } set { _Tips = value; RaisePropertyChanged(() => Tips); } } private string _Role; /// /// 角色 /// public string Role { get { return _Role; } set { _Role = value; RaisePropertyChanged(() => Role); } } private string _TelNumber; /// /// 电话 /// public string TelNumber { get { return _TelNumber; } set { _TelNumber = value; RaisePropertyChanged(() => _TelNumber); } } /// /// 部门名称 /// private string _OrgName; public string OrgName { get { return _OrgName; } set { _OrgName = value; RaisePropertyChanged(() => OrgName); } } private string _ShowRoles; /// /// 作战室角色 /// public string ShowRoles { get { return _ShowRoles; } set { _ShowRoles = value; RaisePropertyChanged(() => ShowRoles); } } private string _OrgId; /// /// 部门编号 /// public string OrgId { get { return _OrgId; } set { _OrgId = value; RaisePropertyChanged(() => OrgId); } } private bool _AllowDelete = true; public bool AllowDelete { get { return _AllowDelete; } set { _AllowDelete = value; RaisePropertyChanged(() => AllowDelete); } } private bool _IsSelected; public bool IsSelected { get { return _IsSelected; } set { _IsSelected = value; RaisePropertyChanged(() => IsSelected); } } private bool _AllowSelected; public bool AllowSelected { get { return _AllowSelected; } set { _AllowSelected = value; RaisePropertyChanged(() => AllowSelected); } } private string _UsersScores="0"; /// /// 作战室成员打分与成员顺序对应 /// public string UsersScores { get { return _UsersScores; } set { _UsersScores = value; RaisePropertyChanged(() => UsersScores); } } }}

大概需求就是展示群组人员信息的一个列表。
一般展示列表,首先想到的是用ListBox控件,当然可以实现,但是这里的这个列表中内容较多,还又一个打分图框操作(已实现),用ListBox自定义控件实现起来会比较麻烦,而且这个列表中的一些内容已经有过实现,综合考虑,最后还是用ItemsControl ,通过数据模板展示MemberInfoModel对象,数据模板的内容用到UserControl(用户控件)。
 

可以看到,数据模板的内容是名为 SummaryScoreResource 的一个UserControl,SummaryScoreResource 中的UserName,OrgName等属性都是通过绑定DataContext.UserName,ElementName=ResourceGrid 对应的属性绑定的。
后台操作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Telewave.CompositeOperations.Views.IMViews
{
    /// 
    /// SummaryScoreResource.xaml 的交互逻辑
    /// 
    public partial class SummaryScoreResource : UserControl
    {
        public SummaryScoreResource()
        {
            InitializeComponent();
        }

        #region 单例

        private static SummaryScoreResource _instance;

        public static SummaryScoreResource Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new SummaryScoreResource();
                }
                return _instance;
            }
        }

        #endregion

        /// 
        ///   姓名
        /// 
        public string UserName
        {
            get { return (string)GetValue(UserNameProperty); }
            set { SetValue(UserNameProperty, value); }
        }

        /// 
        ///   提示信息(部门)
        /// 
        public string OrgName
        {
            get { return (string)GetValue(OrgNameProperty); }
            set { SetValue(OrgNameProperty, value); }
        }
        /// 
        ///   图片路径
        /// 
        public string ImageSource
        {
            get
            {
                return (string)GetValue(ImageSourceProperty);
            }
            set
            {
                SetValue(ImageSourceProperty, value);
            }
        }

        /// 
        /// 登录名(警号)
        /// 
        public string LoginId
        {
            get { return (string)GetValue(LoginIdProperty); }
            set { SetValue(LoginIdProperty, value); }
        }
        /// 
        /// 用户id(guid)
        /// 
        public string UserId
        {
            get { return (string)GetValue(UserIdProperty); }
            set { SetValue(UserIdProperty, value); }
        }

        /// 
        /// 评分间隔值
        /// 
        public string Increment
        {
            get { return (string)GetValue(IncrementProperty); }
            set { SetValue(IncrementProperty, value); }
        }

        public static readonly DependencyProperty IncrementProperty =
     DependencyProperty.Register("Increment", typeof(string), typeof(SummaryScoreResource),
                                 new FrameworkPropertyMetadata(string.Empty,
                                                                      FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// 最大值
        /// 
        public string MaxValue
        {
            get { return (string)GetValue(MaxValueProperty); }
            set { SetValue(MaxValueProperty, value); }
        }

        public static readonly DependencyProperty MaxValueProperty =
     DependencyProperty.Register("MaxValue", typeof(string), typeof(SummaryScoreResource),
                                 new FrameworkPropertyMetadata(string.Empty,
                                                                      FrameworkPropertyMetadataOptions.AffectsRender));

        /// 
        /// 最小值
        /// 
        public string MinValue
        {
            get { return (string)GetValue(MinValueProperty); }
            set { SetValue(MinValueProperty, value); }
        }

        public static readonly DependencyProperty MinValueProperty =
     DependencyProperty.Register("MinValue", typeof(string), typeof(SummaryScoreResource),
                                 new FrameworkPropertyMetadata(string.Empty,
                                                                      FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// 评分值
        /// 
        public string Value
        {
            get { return (string)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        public static readonly DependencyProperty ValueProperty =
     DependencyProperty.Register("Value", typeof(string), typeof(SummaryScoreResource),
                                 new FrameworkPropertyMetadata(string.Empty,
                                                                      FrameworkPropertyMetadataOptions.AffectsRender));
        /// 
        /// 状态颜色
        /// 
        public Brush Colors
        {
            get { return (Brush)GetValue(ColorsProperty); }
            set { SetValue(ColorsProperty, value); }
        }

        public static readonly DependencyProperty ColorsProperty = DependencyProperty.Register("Colors"
            , typeof(Brush), typeof(SummaryScoreResource), new FrameworkPropertyMetadata(new SolidColorBrush(Color.FromRgb(40, 139, 225))));

        /// 
        /// 初始评分
        /// 
        public string Score
        {
            get { return (string)GetValue(ScoreProperty); }
            set { SetValue(ScoreProperty, value); }
        }

        public static readonly DependencyProperty ScoreProperty =
     DependencyProperty.Register("Score", typeof(string), typeof(SummaryScoreResource),
                                 new FrameworkPropertyMetadata(string.Empty,
                                                                      FrameworkPropertyMetadataOptions.AffectsRender));

        public static readonly DependencyProperty LoginIdProperty =
            DependencyProperty.Register("LoginId", typeof(string), typeof(SummaryScoreResource),
                                        new FrameworkPropertyMetadata(string.Empty,
                                                                      FrameworkPropertyMetadataOptions.AffectsRender));
        public static readonly DependencyProperty UserIdProperty =
         DependencyProperty.Register("UserId", typeof(string), typeof(SummaryScoreResource),
                                     new FrameworkPropertyMetadata(string.Empty,
                                                                   FrameworkPropertyMetadataOptions.AffectsRender));
        public static readonly DependencyProperty UserNameProperty =
         DependencyProperty.Register("UserName", typeof(string), typeof(SummaryScoreResource), new FrameworkPropertyMetadata(string.Empty,new PropertyChangedCallback(ImageSourcePropertyChangedCallback)));

        public static readonly DependencyProperty ImageSourceProperty =
DependencyProperty.Register("ImageSource", typeof(string), typeof(SummaryScoreResource),
                         new FrameworkPropertyMetadata(string.Empty,
                                                       FrameworkPropertyMetadataOptions.AffectsRender));

        public static readonly DependencyProperty OrgNameProperty =
DependencyProperty.Register("OrgName", typeof(string), typeof(SummaryScoreResource),
                        new FrameworkPropertyMetadata(string.Empty,
                                                      FrameworkPropertyMetadataOptions.AffectsRender));
        private static void ImageSourcePropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs arg)
        {
        }

    }
}

SummaryScoreResource.xaml


    
        
        
            
            
            
            
        
        
        
            
        
        
            
                
                    
                    
                
                
                
                    
                    
                    
                    
                
                    
                    
                
                    
                
                    
                
                
            
            
        
        
    
    
        
            
            
        

        

    
    
        
    


你可能感兴趣的:(自定义控件)