【WPF】 使用UserControl并在XAML中赋初始值

文章目录

  • 一、前言
  • 二、使用步骤
    • [1. cs中设置控件属性](#1. cs中设置控件属性)
    • [2. 在引入控件时,Xaml传初始值](#2. 在引入控件时,Xaml传初始值)

一、前言

在XAML中使用自定义控件,并在XAML中并赋初始值。

二、使用步骤

1. cs中设置控件属性

设置OriginLocation,ScaleInterval 属性,可以通过XAML内传初始值

c# 复制代码
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

public partial class RadarChart : UserControl
{

    //原点位置
    private static readonly DependencyProperty OriginLocationProperty =
        DependencyProperty.Register("OriginLocation", typeof(string), typeof(RadarChart));
    //刻度间隔
    private static readonly DependencyProperty ScaleIntervalProperty =
        DependencyProperty.Register("ScaleInterval",typeof(int),typeof(RadarChart));
    
    
    


    public string OriginLocation {
        get { return (string)GetValue(OriginLocationProperty); }
        set { SetValue(OriginLocationProperty, value); }
    }

    public int ScaleInterval {
        get { return (int)GetValue(ScaleIntervalProperty); }
        set { SetValue(ScaleIntervalProperty, value); }
    }
 }   

2. 在引入控件时,Xaml传初始值

复制代码
<components:RadarChart OriginLocation="upleft" ScaleInterval="10" />

相关推荐
武藤一雄2 小时前
.NET 中常见计时器大全
microsoft·微软·c#·.net·wpf·.netcore
MarkHD5 小时前
车辆TBOX科普 第70次 AUTOSAR Adaptive、容器化与云原生的融合革命
云原生·wpf
极客智造6 小时前
WPF Behavior 实战:自定义 InvokeCommandAction 实现事件与命令解耦
wpf
L、2186 小时前
Flutter 与 OpenHarmony 深度集成:构建分布式多端协同应用
分布式·flutter·wpf
布伦鸽6 小时前
C# WPF -MaterialDesignTheme 找不到资源“xxx“问题记录
开发语言·c#·wpf
小二·19 小时前
MyBatis基础入门《十五》分布式事务实战:Seata + MyBatis 实现跨服务数据一致性
分布式·wpf·mybatis
helloworddm1 天前
UnregisterManyAsync
wpf
军训猫猫头1 天前
3.NModbus4 长距离多设备超时 C# + WPF 完整示例
c#·.net·wpf·modbus
Aevget1 天前
DevExpress WPF中文教程:Data Grid - 如何绑定到有限制的自定义服务(一)?
ui·.net·wpf·devexpress·ui开发·wpf界面控件
Macbethad1 天前
半导体设备工厂自动化软件技术方案
wpf·智能硬件