0

I have an UserControl that have Object Propdp:

 public partial class HeaderControl : UserControl
    {
        public HeaderControl()
        {
            InitializeComponent();
           DataContext = this;
        }

        public static readonly DependencyProperty ControlContentProperty =
           DependencyProperty.Register(nameof(ControlContent), typeof(object), 
               typeof(HeaderControl), new PropertyMetadata(null));
        public object ControlContent
        {
            get { return (object)GetValue(ControlContentProperty); }
            set { SetValue(ControlContentProperty, value); }
        }
    }

In Xaml file:

<Grid Grid.Row="1">
     <ContentControl Content="{Binding ControlContent, ElementName=root}"/>
</Grid>

if i use it without give a Name to the Button inside it works:

 <ctrl:HeaderContorl >
    <ctrl:HeaderContorl.ControlContent>
                <Button Content="Hallooo" FontSize="40"
                         Foreground="Black" Height="100"/>
    </ctrl:HeaderContorl.ControlContent>
 </ctrl:HeaderContorl>

But if i name the Button inside it not works:

 <ctrl:HeaderContorl >
    <ctrl:HeaderContorl.ControlContent>
                <Button x:Name="Btn" Content="Hallooo" FontSize="40"
                         Foreground="Black" Height="100"/>
    </ctrl:HeaderContorl.ControlContent>
 </ctrl:HeaderContorl>

Can someone please tell me why I can't name the button inside?

  • Out of curiosity, do you know that there already is [HeaderedContentControl](https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.headeredcontentcontrol?view=windowsdesktop-6.0), which you could use as base class of a custom control? – Clemens Jul 27 '22 at 08:34
  • Does this answer your question? [How to create a WPF UserControl with NAMED content](https://stackoverflow.com/questions/751325/how-to-create-a-wpf-usercontrol-with-named-content) – Nawed Nabi Zada Jul 27 '22 at 08:43
  • Hi, i can not use HeaderedContentControl or ControlTemplate for what i need. i want to add the Control inside my HeaderContorl.ControlContent like i want. I just have problem to name the Controls inside it. And i just ask for help why i can´t name the Controls inside the Object dependency property ControlContent of the HeaderControl. thanks – christophe etse Jul 27 '22 at 11:22

0 Answers0