Propriété personnalisée de Contrôle de l'Utilisateur n'est pas accessible dans WPF C#

0

La question

Je veux créer un contrôle Utilisateur (UserControl) avec la propriété personnalisée (MyLabel) dans WPF à l'aide de C# sans écrire de code derrière. Mais ma propriété personnalisée MyLabel est inaccessible dans la MainWindow.xaml quand je suis avec mon contrôle personnalisé. Quel est le problème dans mon code? Si mon application est mal alors comment faire?

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

La meilleure réponse

0

L'expression

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

exige que MyLabel est une propriété de la UserControl1 classe.

Vous devez déclarer la propriété de la déclaration de classe de UserControl1, c'est à dire dans le fichier 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); }
    }
}

Vous lier à la propriété dans l'objet UserControl XAML par

<TextBlock Text="{Binding MyLabel,
                  RelativeSource={RelativeSource AncestorType=UserControl}}" />
2021-11-21 17:45:11

Dans d'autres langues

Cette page est dans d'autres langues

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................