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

相关推荐
七夜zippoe5 小时前
DolphinDB在工业物联网中的优势
物联网·wpf·工业物联网·优势·dolphindb
heimeiyingwang8 小时前
【架构实战】观察者模式在分布式系统中的应用
观察者模式·架构·wpf
bugcome_com10 小时前
WPF + Microsoft.ToolKit.Mvvm 技术指南与实战项目
microsoft·wpf
武藤一雄1 天前
WPF中逻辑树(Logical Tree)与可视化树(Visual Tree)到底是什么
microsoft·c#·.net·wpf·.netcore
炸炸鱼.1 天前
ELK 企业级日志分析系统完整部署手册
elk·wpf
Mr_pyx2 天前
微服务可观测性实战:分布式链路追踪从入门到精通
wpf
c#上位机3 天前
wpf附加事件
wpf
玖笙&3 天前
✨WPF编程进阶【9.1】:WPF资源完全指南(附源码)
c++·c#·wpf·visual studio
想你依然心痛3 天前
HarmonyOS 6(API 23)分布式实战:基于悬浮导航与沉浸光感的“光影协创“跨设备白板系统
分布式·wpf·harmonyos·悬浮导航·沉浸光感
c#上位机5 天前
wpf路由事件
wpf