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

相关推荐
△曉風殘月〆9 小时前
如何在WPF中使用 Fluent 主题
wpf
△曉風殘月〆9 小时前
不同.NET版本中的WPF新增功能
.net·wpf
海盗12349 小时前
WPF使用内置资源系统实现国际化
wpf
Rotion_深9 小时前
WPF UserControl 和 CustomControl
wpf
SEO-狼术10 小时前
Run Secure SFTP Across Every Platform
pdf·wpf
c#上位机1 天前
wpf之RadialGradientBrush径向渐变画刷
wpf
不懂的浪漫1 天前
OpenTelemetry 和 SkyWalking Agent 怎么选?一次讲清 OTel、SkyWalking Agent 的相同点与区别
wpf·skywalking·链路追踪·opentelemetry·otel
c#上位机1 天前
wpf之LinearGradientBrush线性渐变
wpf
暖馒2 天前
WPF绑定由简到繁深入笔记
笔记·wpf
东方.既白2 天前
WPF炫酷界面DEMO
wpf