WPF用户控件

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值