简体   繁体   English

在 WPF C# 中无法访问用户控件的自定义属性

[英]Custom Property of User Control not accessible in WPF C#

I want to create a Custom User control(UserControl1) with custom property(MyLabel) in WPF using C# without writing any code behind.我想使用 C# 在 WPF 中创建一个具有自定义属性(MyLabel)的自定义用户控件(UserControl1),而无需在后面编写任何代码。 But my custom property "MyLabel" is inaccessible when I am using my custom control in MainWindow.xaml.但是当我在 MainWindow.xaml 中使用我的自定义控件时,我的自定义属性“MyLabel”无法访问。 What is the problem in my code?我的代码有什么问题? If my implementation is wrong then how to achieve this?如果我的实现是错误的,那么如何实现呢?

UCControl.cs UCControl.cs

            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Text;
            using System.Threading.Tasks;
            using System.Windows;
            using System.Windows.Controls;
            
            namespace WpfApp1
            {
                public class UCControl:UserControl
                {
                    public String MyLabel
                    {
                        get { return (String)GetValue(MyLabelProperty); }
                        set { SetValue(MyLabelProperty, value); }
                    }
            
                    public static readonly DependencyProperty MyLabelProperty = DependencyProperty.Register("MyLabel", typeof(string), typeof(UCControl),new PropertyMetadata(""));
            
                    public UCControl()
                    {
                        MyLabel = "default label";
                    }
                }
            }

UserControl1.xaml UserControl1.xaml

<UserControl x:Class="WpfApp1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApp1"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Grid.DataContext>
            <local:UCControl/>
        </Grid.DataContext>
        <TextBlock Text="{Binding MyLabel}" FontSize="18"/>
    </Grid>
</UserControl>

MainWindow.xaml主窗口.xaml

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid >
        <local:UserControl1 MyLabel="Hello World"/>
    </Grid>
</Window>

The expression表达方式

<local:UserControl1 MyLabel="Hello World"/>

requires that MyLabel is a property of the UserControl1 class.要求MyLabelUserControl1 class 的属性。

You have to declare the property of the class declaration of UserControl1, ie in the file UserControl1.xaml.cs :您必须声明 UserControl1 的 class 声明的属性,即在文件UserControl1.xaml.cs中:

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty MyLabelProperty =
        DependencyProperty.Register(
            nameof(MyLabel),
            typeof(string),
            typeof(UserControl1));

    public string MyLabel
    {
        get { return (string)GetValue(MyLabelProperty); }
        set { SetValue(MyLabelProperty, value); }
    }
}

You would bind to that property in the UserControl's XAML by您将通过以下方式绑定到 UserControl 的 XAML 中的该属性

<TextBlock Text="{Binding MyLabel,
                  RelativeSource={RelativeSource AncestorType=UserControl}}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM