Proprietà personalizzata di Controllo Utente non è accessibile in WPF C#

0

Domanda

Voglio creare un controllo Utente Personalizzato (UserControl) con la proprietà personalizzata (MyLabel) in WPF C# senza scrivere alcun codice dietro. Ma la mia proprietà personalizzata MyLabel è inaccessibile in MainWindow.xaml quando sto usando il mio controllo personalizzato. Qual è il problema nel mio codice? Se la mia realizzazione è sbagliato allora come raggiungere questo obiettivo?

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

Migliore risposta

0

L'espressione

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

richiede che MyLabel è una proprietà del UserControl1 classe.

Devi dichiarare la proprietà della dichiarazione di classe di UserControl1, cioè nel file 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); }
    }
}

Si associa alla proprietà in oggetto UserControl XAML da

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

In altre lingue

Questa pagina è in altre lingue

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