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

Posted

技术标签:

【中文标题】在 WPF C# 中无法访问用户控件的自定义属性【英文标题】:Custom Property of User Control not accessible in WPF C# 【发布时间】:2021-12-31 12:38:48 【问题描述】:

我想使用 C# 在 WPF 中创建一个具有自定义属性 (MyLabel) 的自定义用户控件 (UserControl),而无需在后面编写任何代码。但是当我使用自定义控件时,我的自定义属性 MyLabelMainWindow.xaml 中无法访问。我的代码有什么问题?如果我的实现是错误的,那么如何实现呢?

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

<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

<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>

【问题讨论】:

public class UCControl:UserControl&lt;UserControl x:Class="WpfApp1.UserControl1" ...&gt;后面的代码 UCControl 与 UserControl1 不匹配。 除此之外,写Text="Binding MyLabel, RelativeSource=RelativeSource AncestorType=UserControl" @Clemens 我没有得到你的第一条评论。 您向我们展示了名为 UCControl 的 UserControl 背后的代码,但向我们展示了名为 UserControl1 的 UserControl 的 XAML。这怎么搭配?如果这真的是你的代码,它看起来真的很奇怪。将 MyLabel 属性移到 UserControl1 后面的代码中,即移到文件 UserControl1.xaml.cs 中,不要在 UserControl1.xaml 中设置 Grid 的 DataContext 属性。 【参考方案1】:

表达式

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

要求MyLabelUserControl1 类的属性。

你必须声明UserControl1的类声明的属性,即在文件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); 
    

您可以通过

绑定到 UserControl 的 XAML 中的该属性
<TextBlock Text="Binding MyLabel,
                  RelativeSource=RelativeSource AncestorType=UserControl" />

【讨论】:

以上是关于在 WPF C# 中无法访问用户控件的自定义属性的主要内容,如果未能解决你的问题,请参考以下文章

如何从自定义用户控件 WPF、C# 中的枚举自定义属性中获取值?

wpf中动态添加的自定义控件过宽,不能完全显示,怎么办

C#中如何实现WPF调用Winform中用户自定义的控件呢?

设置绑定到 WPF 用户控件内的自定义 DependencyProperty

我的自定义控件在页面中为何不能显示?

无法访问用户控制公共属性