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

相关推荐
天才奇男子1 小时前
LVS原理及部署
linux·运维·云原生·wpf·lvs·linux chrony
予枫的编程笔记4 小时前
【Redis实战进阶篇1】Redis 分布式锁:从手写实现到 Redisson 最佳实践
redis·分布式·wpf
小北方城市网1 天前
Spring Cloud Gateway 生产级实践:高可用架构、灰度发布与故障排查
spring boot·redis·分布式·缓存·架构·wpf
ujainu1 天前
Flutter for OpenHarmonyOS 前置知识:Dart语言详解(下)
flutter·wpf·harmonyos
bugcome_com1 天前
WPF 数据模板(DataTemplate):优雅实现数据与 UI 的解耦
ui·wpf
小北方城市网2 天前
Redis 分布式锁与缓存三大问题解决方案
spring boot·redis·分布式·后端·缓存·wpf·mybatis
Aevget2 天前
DevExpress WPF中文教程:Data Grid - 如何绑定到有限制的自定义服务(五)?
wpf
yangSnowy2 天前
Redis数据类型
数据库·redis·wpf
资深web全栈开发2 天前
分布式锁的陷阱:Redlock 真的安全吗?
分布式·安全·wpf
James.TCG2 天前
VM访问View(Interaction)
wpf