กำหนดเองทรัพย์สินของผู้ใช้ควบคุมไม่สามารถเข้าใช้ใน WPF C#

0

คำถาม

ฉันอยากจะสร้างกำหนดเองของผู้ใช้ควบคุม(UserControl)กับกำหนดเองทรัพย์สิน(MyLabel)ใน WPF ใช้ C#โดยไม่มีการเขียนมีรหัสไว้ข้างหลัง แต่ฉันกำหนดเองทรัพย์สิน MyLabel คือเข้าใช้งานไม่ได้อยู่ใน หน้าต่างหลัก.xaml ตอนที่ฉันกำลังใช้กำหนดเองควบคุมไม่ได้ อะไรคือปัญหาของฉันอยู่ในนรหัสหรอ? ถ้าฉัน implementation มันผิดแล้วว่าต้องก็จะประสบความสำเร็จในส่วนนี้?

UCControl.à™àà§à•à£àà™

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>

หน้าต่างหลัก.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,i.e. อยู่ในแฟ้ม 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}}" />
2021-11-21 17:45:11

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

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

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่