【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" />

相关推荐
会飞的大可5 小时前
Spring Cloud Alibaba全景:Nacos、Sentinel、Seata整合实战
sentinel·wpf
baivfhpwxf202310 小时前
DataGrid 中增加选择列 功能实现
ui·wpf
czhc114007566313 小时前
winform 330 跨线程 异步
wpf·线程·winform
想你依然心痛14 小时前
HarmonyOS 5.0教育行业解决方案:基于分布式能力的沉浸式智慧课堂系统
分布式·wpf·harmonyos
Maybe_ch16 小时前
深度解析 WPF 线程模型:告别 UI 卡死,掌握 Dispatcher 核心机制
ui·wpf
code bean16 小时前
【Halcon 】用 Halcon 实现涂抹:Region、仿射变换与 WPF 交互
wpf·交互·halcon
白露与泡影2 天前
Spring Cloud进阶--分布式权限校验OAuth2
分布式·spring cloud·wpf
枫叶丹42 天前
【HarmonyOS 6.0】ArkData 分布式数据对象新特性:资产传输进度监听与接续传输能力深度解析
开发语言·分布式·华为·wpf·harmonyos
一念春风2 天前
智能文字识别工具(AI)
开发语言·c#·wpf
故事不长丨2 天前
WPF MvvmLight 超详细使用教程
c#·wpf·mvvm·mvvmlight