Panel de comunicación de control personalizado:
Panel de comunicación.xaml
<UserControl x:Class="Views.Broadcasting.CommunicationPanel"
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"
xmlns:local="clr-namespace:Views.Broadcasting"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid Background="#385775" Width="200px" Height="80px" Margin="20">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<CheckBox x:Name="Cb_Name" Content="" Foreground="White" FontSize="16" Grid.Row="0" Grid.Column="0" Margin="10,0,0,0" VerticalAlignment="Center"/>
<Label x:Name="Lb_Details" Content="" Foreground="White" FontSize="16" Grid.Row="1" Grid.Column="0" Margin="10,0,0,0"/>
<Image Source="/LS.IntelligentMine.Admin;component/Views/Broadcasting/images/OnLine.png" Grid.Row="0" Margin="0,10,0,0" Grid.Column="1" />
<Label x:Name="Lb_State" Content="" Foreground="White" FontSize="16" Grid.Row="1" Grid.Column="1" Margin="0,0,0,0" HorizontalAlignment="Center" VerticalAlignment="Top"/>
</Grid>
</UserControl>
CommunicationPanel.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
namespace Views.Broadcasting
{
/// <summary>
/// CommunicationPanel.xaml 的交互逻辑
/// </summary>
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);
}
}
}
Controles utilizados en la interfaz Ver
<ItemsControl ItemsSource="{Binding jkDeviceItems}">
<ItemsControl.ItemsPanel >
<ItemsPanelTemplate>
<UniformGrid Columns="6" Margin="0" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<loc:CommunicationPanel TerminalNO="{Binding TerminalNO}" Location="{Binding Location}" Status="{Binding Status}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:CallMethodAction MethodName="CustomCheckBox_Checked" TargetObject="{Binding DataContext, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
</i:EventTrigger>
<i:EventTrigger EventName="UnChecked">
<i:CallMethodAction MethodName="CustomCheckBox_UnChecked" TargetObject="{Binding DataContext, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</loc:CommunicationPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Código de modelo de vista
Declare jkDeviceItems, omitiré la asignación
public ObservableCollection<TerminalInfo> jkDeviceItems { get; set; } = new ObservableCollection<TerminalInfo>();
evento de escritura
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
{
}
}
Lo anterior es el código de implementación, y no hablaré sobre la descripción detallada debido a limitaciones de tiempo. Si tiene alguna pregunta, puede enviar un mensaje privado o comentar.