WPF 精修篇 自定义控件

原文: WPF 精修篇 自定义控件

自定义控件 因为没有办法对界面可视化编辑 所以用来很少 

现在实现的是 自定义控件的 自定义属性 和自定义方法

用VS 创建自定义控件后 会自动创建 Themes 文件夹和 Generic.xaml 还有自定义的类 这边是SeachControl

Gneneric


  
  
  1. <Style TargetType= "{x:Type local:SeachControl}">
  2. <Setter Property= "Template">
  3. <Setter.Value>
  4. <ControlTemplate TargetType= "{x:Type local:SeachControl}">
  5. <Grid>
  6. <StackPanel Orientation= "Horizontal" >
  7. <TextBox Width= "100" Height= "20" Margin= "0,0,5,0" Text= "{Binding SearchText, RelativeSource={RelativeSource Mode=TemplatedParent},Mode=TwoWay}" Background= "{TemplateBinding Background}"></TextBox>
  8. <Button x:Name= "button" Content= "Select" Width= "50" Height= "20" ></Button>
  9. </StackPanel>
  10. </Grid>
  11. </ControlTemplate>
  12. </Setter.Value>
  13. </Setter>
  14. </Style>

自定义控件类


  
  
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. namespace WpfApplication24
  15. {
  16. /// <summary>
  17. /// 按照步骤 1a 或 1b 操作,然后执行步骤 2 以在 XAML 文件中使用此自定义控件。
  18. ///
  19. /// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。
  20. /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
  21. /// 元素中:
  22. ///
  23. /// xmlns:MyNamespace="clr-namespace:WpfApplication24"
  24. ///
  25. ///
  26. /// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
  27. /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
  28. /// 元素中:
  29. ///
  30. /// xmlns:MyNamespace="clr-namespace:WpfApplication24;assembly=WpfApplication24"
  31. ///
  32. /// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用,
  33. /// 并重新生成以避免编译错误:
  34. ///
  35. /// 在解决方案资源管理器中右击目标项目,然后依次单击
  36. /// “添加引用”->“项目”->[浏览查找并选择此项目]
  37. ///
  38. ///
  39. /// 步骤 2)
  40. /// 继续操作并在 XAML 文件中使用控件。
  41. ///
  42. /// <MyNamespace:SeachControl/>
  43. ///
  44. /// </summary>
  45. public class SeachControl : Control
  46. {
  47. static SeachControl()
  48. {
  49. DefaultStyleKeyProperty.OverrideMetadata( typeof(SeachControl), new FrameworkPropertyMetadata( typeof(SeachControl)));
  50. }
  51. public string SearchText
  52. {
  53. get { return ( string)GetValue(SearchTextProperty); }
  54. set { SetValue(SearchTextProperty, value); }
  55. }
  56. // Using a DependencyProperty as the backing store for SearchText. This enables animation, styling, binding, etc...
  57. public static readonly DependencyProperty SearchTextProperty =
  58. DependencyProperty.Register( "SearchText", typeof( string), typeof(SeachControl), new PropertyMetadata( ""));
  59. public delegate void OnSeachClick(object ob, SearchEventArgs args);
  60. public event OnSeachClick SeachButtenClick;
  61. public override void OnApplyTemplate()
  62. {
  63. base.OnApplyTemplate();
  64. var button = GetTemplateChild( "button");
  65. if (button is Button)
  66. {
  67. (button as Button).Click += SeachControl_Click;
  68. }
  69. }
  70. void SeachControl_Click(object sender, RoutedEventArgs e)
  71. {
  72. if (SeachButtenClick != null)
  73. {
  74. SeachButtenClick.Invoke( this, new SearchEventArgs() { SreachItem = SearchText });
  75. }
  76. }
  77. }
  78. }

创建参数类


  
  
  1. public class SearchEventArgs: EventArgs
  2. {
  3. public string SreachItem { get; set; }
  4. }

main 引用 这里可以看到自定义的事件


  
  
  1. <Window x:Class= "WpfApplication24.MainWindow"
  2. xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x= "http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:MyNamespace= "clr-namespace:WpfApplication24"
  5. Title= "MainWindow" Height= "350" Width= "525">
  6. <Window.Resources>
  7. </Window.Resources>
  8. <Grid>
  9. <MyNamespace:SeachControl HorizontalAlignment= "Center" SearchText= "嗯嗯" VerticalAlignment= "Center" Background= "#FFCBCBCB" SeachButtenClick= "SeachControl_SeachButtenClick" />
  10. </Grid>
  11. </Window>

Main的内容类

扫描二维码关注公众号,回复: 8259007 查看本文章

  
  
  1. private void SeachControl_SeachButtenClick(object ob, SearchEventArgs args)
  2. {
  3. MessageBox.Show( "HI "+ args.SreachItem);
  4. }

猜你喜欢

转载自www.cnblogs.com/lonelyxmas/p/12075459.html