사용자 정의 재산의 사용자 정의 컨트롤에 액세스할 수 없습 WPF C#

0

질문

내가 만들려면 사용자 정의 사용자 정의 컨트롤(UserControl는)사용자 정의 속성(MyLabel에서)WPF C#을 사용하여 코드를 작성하지 않고 뒤에 있습니다. 하지만 나의 주문성 MyLabel 액세스할 수 없에 MainWindow.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>
1

최고의 응답

0

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

필요MyLabel 의 속성 UserControl1 클래스입니다.

당신이 선언하의 속성 클래스를 선언 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); }
    }
}

당신이에 바인딩하는 속성에 정렬의 XAML 여

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

다른 언어로

이 페이지는 다른 언어로되어 있습니다

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