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

相关推荐
jyl_sh2 小时前
Ribbon布局和尺寸调整
ribbon·c#·wpf·客户端
△曉風殘月〆1 天前
在WPF中保存控件内容为图片
wpf
芝麻科技1 天前
Wpf使用NLog将日志输出到LogViewer
wpf·prism
△曉風殘月〆2 天前
WPF颜色(SolidColorBrush)和Win32颜色(COLOREF)互转的方法
wpf·win32·solidcolorbrush·colorref
shanshan20992 天前
上位机系统架构 | 如何设计一个高效的多相机管理系统
c#·wpf·相机
充值内卷2 天前
WPF入门教学四 WPF控件概述
windows·ui·wpf
小白3 天前
WPF自定义Dialog模板,内容用不同的Page填充
wpf
Crazy Struggle3 天前
C# + WPF 音频播放器 界面优雅,体验良好
c#·wpf·音频播放器·本地播放器
James.TCG4 天前
WPF动画
wpf
He BianGu4 天前
笔记:简要介绍WPF中FormattedText是什么,主要有什么功能
笔记·c#·wpf