在UniformGrid布局中动态生成自定义控件,并给自定义控件赋值、绑定事件
自定义控件CommunicationPanel:
CommunicationPanel.xaml
CommunicationPanel.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;namespace Views.Broadcasting
{/// /// CommunicationPanel.xaml 的交互逻辑/// public partial class CommunicationPanel : UserControl{public CommunicationPanel(){InitializeComponent();}public string TerminalNO{get { return (string)GetValue(TerminalNOProperty); }set { SetValue(TerminalNOProperty, value); }}public static readonly DependencyProperty TerminalNOProperty =DependencyProperty.Register("TerminalNO", typeof(string), typeof(CommunicationPanel), new PropertyMetadata(TerminalNOPropertyChanged));private static void TerminalNOPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){CommunicationPanel myControl = d as CommunicationPanel;if (myControl != null){myControl.Cb_Name.Content=e.NewValue.ToString();}}public string Location{get { return (string)GetValue(LocationProperty); }set { SetValue(LocationProperty, value); }}public static readonly DependencyProperty LocationProperty =DependencyProperty.Register("Location", typeof(string), typeof(CommunicationPanel), new PropertyMetadata(LocationPropertyChanged));private static void LocationPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){CommunicationPanel myControl = d as CommunicationPanel;if (myControl != null){myControl.Lb_Details.Content = e.NewValue.ToString();}}public string Status{get { return (string)GetValue(StatusProperty); }set { SetValue(StatusProperty, value); }}public static readonly DependencyProperty StatusProperty =DependencyProperty.Register("Status", typeof(string), typeof(CommunicationPanel), new PropertyMetadata(StatusPropertyChanged));private static void StatusPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){CommunicationPanel myControl = d as CommunicationPanel;if (myControl != null){var val = e.NewValue.ToString();if (val == "0") {myControl.Lb_State.Content = "空闲";}else if (val == "1"){myControl.Lb_State.Content = "广播中";}else if (val == "2"){myControl.Lb_State.Content = "拨号中";}else if (val == "3"){myControl.Lb_State.Content = "对讲中";}else if (val == "4"){myControl.Lb_State.Content = "喊话中";}else if (val == "5"){myControl.Lb_State.Content = "监听中";}else if (val == "6"){myControl.Lb_State.Content = "断线";}}}public bool? Cb_IsChecked{get{return Cb_Name.IsChecked;}set{Cb_Name.IsChecked = value;}}public event EventHandler Checked;public event EventHandler UnChecked;public static readonly DependencyProperty IsCheckedProperty =DependencyProperty.Register("IsChecked", typeof(bool), typeof(CommunicationPanel), new PropertyMetadata(false));public bool IsChecked{get { return (bool)GetValue(IsCheckedProperty); }set { SetValue(IsCheckedProperty, value); }}public override void OnApplyTemplate(){base.OnApplyTemplate();// 找到CheckBox并添加事件处理程序CheckBox checkBox = this.FindName("Cb_Name") as CheckBox;if (checkBox != null){checkBox.Checked += CheckBox_Checked;checkBox.Unchecked += CheckBox_UnChecked;}}private void CheckBox_UnChecked(object sender, RoutedEventArgs e){IsChecked = false;// 触发取消事件UnChecked?.Invoke(this, EventArgs.Empty);}private void CheckBox_Checked(object sender, RoutedEventArgs e){IsChecked = false;// 触发取消事件Checked?.Invoke(this, EventArgs.Empty);}}
}
View界面中使用控件
ViewModel代码
声明 jkDeviceItems,赋值我就省略不写了
public ObservableCollection jkDeviceItems { get; set; } = new ObservableCollection();
写事件
public void CustomCheckBox_Checked(object sender, EventArgs e){// 处理选中事件CommunicationPanel communicationPanel= sender as CommunicationPanel;if (communicationPanel.Cb_IsChecked == true){}else {}}public void CustomCheckBox_UnChecked(object sender, EventArgs e){// 处理取消事件CommunicationPanel communicationPanel = sender as CommunicationPanel;if (communicationPanel.Cb_IsChecked == true){}else{}}
以上就是实现代码,详细说明由于时间原因就不说了,有疑问可以私信或评论。
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
