कस्टम संपत्ति के उपयोगकर्ता नियंत्रण में सुलभ नहीं WPF C#

0

सवाल

मैं बनाने के लिए चाहते हैं एक कस्टम उपयोगकर्ता नियंत्रण (UserControl) के साथ कस्टम संपत्ति (MyLabel) WPF में सी# का उपयोग कर के बिना किसी भी कोड लिखने के पीछे. लेकिन मेरे कस्टम गुण MyLabel में दुर्गम है MainWindow.xaml जब मैं कर रहा हूँ का उपयोग कर अपने कस्टम नियंत्रण । समस्या क्या है मेरे कोड में? अगर मेरे कार्यान्वयन गलत है तो फिर कैसे करने के लिए इस लक्ष्य को हासिल?

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>

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); }
    }
}

आप बाइंड करने के लिए है कि संपत्ति में 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ý
..................................................................................................................

इस श्रेणी में लोकप्रिय

लोकप्रिय सवाल इस श्रेणी में