WPF 用户控件的写法,前端代码
<UserControl x:Class="wenshi"
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:动环主界面"
mc:Ignorable="d"
d:DesignHeight="260" d:DesignWidth="200">
<Grid Cursor="Hand">
<Grid.ToolTip>
<StackPanel Background="Azure" Height="200" Width="200">
<StackPanel Background="LightBlue" Height="30" Width="200" Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock>
<Run FontFamily="微软雅黑" FontSize="18" FontWeight="Normal" Text="天使的翅膀"/>
</TextBlock>
</StackPanel>
<StackPanel Width="200">
<TextBlock TextWrapping="Wrap" Width="100" Height="98" HorizontalAlignment="Center" VerticalAlignment="Top">
<Run FontFamily="Verdana" FontSize="11" Foreground="Black" Text="落叶随风将要去何方只留给天空美丽场"/>
</TextBlock>
</StackPanel>
</StackPanel>
</Grid.ToolTip>
<Border BorderThickness="1" BorderBrush="#FF8D8D93" CornerRadius="10,10,10,10" Background="#FF13335C"/>
<Label x:Name="label" Content="Label" HorizontalAlignment="Left" Height="28" Margin="26,10,0,0" VerticalAlignment="Top" Width="105" Foreground="#FFFDFDFD"/>
<Label x:Name="label1" Content="温度" HorizontalAlignment="Left" Height="24" Margin="10,43,0,0" VerticalAlignment="Top" Width="37" Foreground="#FFFDFDFD"/>
<Label x:Name="label1_Copy" Content="湿度" HorizontalAlignment="Left" Height="24" Margin="10,78,0,0" VerticalAlignment="Top" Width="37" Foreground="#FFFDFDFD"/>
<Label x:Name="label_Copy" Content="待获取" HorizontalAlignment="Left" Height="28" Margin="26,122,0,0" VerticalAlignment="Top" Width="124" Foreground="#FFFDFDFD"/>
<TextBox x:Name="textBox_wendu" HorizontalAlignment="Left" Height="23" Margin="56,45,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="75" IsEnabled="False"/>
<TextBox x:Name="textBox_shidu" HorizontalAlignment="Left" Height="23" Margin="58,78,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="73" IsEnabled="False"/>
</Grid>
</UserControl>
后端代码
Public Class wenshi
Inherits UserControl
Private ReadOnly _colorBlue As Color = Color.FromRgb(0, 255, 200)
Private ReadOnly _colorRed As Color = Color.FromRgb(255, 0, 0)
Public Sub New()
InitializeComponent()
End Sub
Public _id As Integer = -1
Public Sub setContent(ByVal id As Integer)
Name = "自定义控件" + id
label.Content = id
_id = id
End Sub
Public Sub SetValue(ByVal wendu_num As Double, ByVal shidu_num As Double)
textBox_wendu.Text = wendu_num
textBox_shidu.Text = shidu_num
End Sub
Public Sub setColor(ByVal i As Integer)
If i = 0 Then
Background = New SolidColorBrush(_colorBlue)
End If
If i = 1 Then
Background = New SolidColorBrush(_colorRed)
End If
End Sub
End Class
调用方式
<Grid >
<Border BorderThickness="1" BorderBrush="#FF8D8D93" CornerRadius="10,10,10,10" Background="#FF13335C"/>
<ScrollViewer x:Name="scrList" Margin="0" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<WrapPanel x:Name="wpmain" Width="1060"/>
</ScrollViewer>
</Grid>
Class wenshidu
Public controls As New List(Of wenshi)
Public Sub tianjia(ByVal num As Integer)
If num > 0 Then
For i = 1 To num
Dim wenshict As New wenshi
wpmain.Children.Add(wenshict)
controls.Add(wenshict)
Next
End If
End Sub
End Class