wpf 用户控件

实现目标

dbeaver 新建连接时选择数据库类型控件

在这里插入图片描述

参考资料

自定义控件: https://www.cnblogs.com/dotnet261010/p/6372222.html

新建用户控件

在这里插入图片描述

xaml

<UserControl x:Class="database_manager.DatabaseItemSelectorButton"
             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:khl="clr-namespace:database_manager"
             mc:Ignorable="d" 
             d:DesignHeight="110" d:DesignWidth="100" Padding="5" MouseEnter="selector_MouseEnter" MouseLeave="selector_MouseLeave" MouseDown="selector_MouseDown">
    <StackPanel Orientation="Vertical">
        <Image Name="image" Source="{Binding Path=ImagePath, RelativeSource= {RelativeSource AncestorType={x:Type khl:DatabaseItemSelectorButton }}}" Height="70"/>
        <!--<Label Name="label" Margin="0 5 0 0" Content="{Binding Path=DatabaseName, RelativeSource= {RelativeSource AncestorType={x:Type khl:DatabaseItemSelectorButton }}}" HorizontalContentAlignment="Center" Foreground="#ffffff"  />-->
        <Label Name="label" Margin="0 5 0 0" Content="{Binding DatabaseName}" HorizontalContentAlignment="Center" />
    </StackPanel>
</UserControl>

cs

using log4net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace database_manager
{
   
    /// <summary>
    /// DatabaseItemSelectorButton.xaml 的交互逻辑
    /// </summary>
    public partial class DatabaseItemSelectorButton : UserControl
    {
   
        ILog log = LogManager.GetLogger(typeof(DatabaseItemSelectorButton));

        /// <summary>
        /// 选中状态背景色
        /// </summary>
        private const string SELECT_BG = "#0078D7";

        /// <summary>
        /// 未选中状态悬停背景色
        /// </summary>
        private const string UN_SELECT_BG = "#4CA0E3";

        /// <summary>
        /// 鼠标悬停标签颜色
        /// </summary>
        private const string LABEL_HOVER_COLOR = "#FFFFFF";

        /// <summary>
        /// 鼠标离开标签颜色
        /// </summary>
        private const string LABEL_NORMAL_COLOR = "#000000";

        /// <summary>
        /// 数据库名称
        /// </summary>
        public static readonly DependencyProperty DatabaseNameProperty = DependencyProperty.Register("DatabaseName",
            typeof(string), typeof(DatabaseItemSelectorButton), new PropertyMetadata("", new PropertyChangedCallback(OnDatabaseNamePropertyChange)));

        /// <summary>
        /// 图片
        /// </summary>
        public static readonly DependencyProperty ImagePathProperty = DependencyProperty.Register("ImagePath",
            typeof(string), typeof(DatabaseItemSelectorButton), new PropertyMetadata("", new PropertyChangedCallback(OnImagePathPropertyChange)));

        /// <summary>
        /// 选中状态变化事件
        /// </summary>
        public static readonly RoutedEvent SelectedChangedEvent = EventManager.RegisterRoutedEvent("SelectedChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(DatabaseItemSelectorButton));

        /// <summary>
        /// 是否选中
        /// </summary>
        public bool Checked {
    get; set; }

        /// <summary>
        /// 图片路径
        /// </summary>
        public string ImagePath
        {
   
            get {
    return 
WPF中,用户控件(User Control)是一种可重用的UI元素,它们允许你封装代码和布局,并可以在应用程序的不同地方多次使用。如果需要在创建用户控件实例时传递参数,通常可以通过以下几种方式实现: 1. **构造函数参数**: - 在用户控件的XAML中,可以声明一个构造函数并接收参数,然后在对应的`DataContext`设置上绑定这些参数。例如: ```xml <my:MyUserControl Parameter1="Value1" Parameter2="Value2" /> ``` - 在`MyUserControl`的`InitializeComponent`方法中,通过`DependencyObject.GetValue`获取这些值。 2. **依赖属性(Dependency Properties)**: - 你可以定义依赖属性,并在构造函数或`OnApplyTemplate`中设置这些属性的值。这样,可以通过XAML直接传递参数,或者在运行时动态设置。 ```csharp public static readonly DependencyProperty ParameterProperty = DependencyProperty.Register("Parameter", typeof(string), typeof(MyUserControl)); public string Parameter { get { return (string)GetValue(ParameterProperty); } set { SetValue(ParameterProperty, value); } } ``` 3. **Command属性与命令参数**: - 如果用户控件处理一些操作,可以使用`Command`属性,并在命令中传递参数。这样可以通过命令触发器传递参数。 4. **事件总线或消息传递机制**: - 如果应用范围较大,可以考虑使用MVVM架构,通过事件总线或消息传递机制,将参数作为事件发送到特定的用户控件实例。 使用上述方法之一时,请确保在`OnInitialized`或`OnApplyTemplate`等适当生命周期方法中正确地设置和处理这些参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值