0
votes

I'm creating a WPF program and I have created a custom Usercontrol and custom Textbox

When I rebuild my solution in visual studio i get this error.

Cannot set Name attribute value 'SearchT' on element 'HintTextBox'. 'HintTextBox' is under the scope of element 'ClickableControl', which already had a name registered when it was defined in another scope

I don't know what I need to do. Or what I did wrong? can someone help me? The classes below are the usercontrol and the hinttextbox, the last one is how I implmented them in xaml.

This is how I put the textbox in my Usercontrol

TEXTBOX = HintTextBox:

namespace View.custom_usercontrols { public partial class HintTextBox : TextBox { public static readonly DependencyProperty HintepDependencyProperty = DependencyProperty.Register("Hint", typeof(string), typeof(HintTextBox));

    public string Hint
    {
        get
        {
            return (string)GetValue(HintepDependencyProperty);
        }
        set
        {
            SetValue(HintepDependencyProperty, value);
        }
    }   
    private string _text;
    private bool _placeHolder;
    public HintTextBox()
    {
        InitializeComponent();
        if (Hint == null)
        {
           _text = "";
        }
        else
        {
            _text = Hint;
        }
        _placeHolder = true;
        Text = _text;
        Opacity = 0.2;
    }
    //extra code
 }

}

This is my UserControl = ClickableControl

namespace View.custom_usercontrols
{
    [ContentProperty(nameof(Children))]
    public partial class ClickableControl : UserControl
    {
        public static readonly DependencyPropertyKey ChildrenProperty = DependencyProperty.RegisterReadOnly(
            nameof(Children),  // Prior to C# 6.0, replace nameof(Children) with "Children"
            typeof(UIElementCollection),
            typeof(ClickableControl),
            new PropertyMetadata());

        public static readonly DependencyProperty HoverColorDependencyProperty = DependencyProperty.Register("HoverColor", typeof(Brush), typeof(HintTextBox));

        public static readonly DependencyProperty SelectedColorDependencyProperty = DependencyProperty.Register("SelectedColor", typeof(Brush), typeof(HintTextBox));
        public static readonly DependencyProperty SelectedDependencyProperty = DependencyProperty.Register("Selected", typeof(Boolean), typeof(HintTextBox));

        public Brush HoverColor
        {
            get
            {
                return (Brush)GetValue(HoverColorDependencyProperty);
            }
            set
            {
                SetValue(HoverColorDependencyProperty, value);
            }
        }
        public Brush SelectedColor
        {
            get
            {
                return (Brush)GetValue(SelectedColorDependencyProperty);
            }
            set
            {
                SetValue(SelectedColorDependencyProperty, value);
            }
        }
        private Brush BackgroundColor { get; set; }
        public Boolean Selected
        {
            get
            {
                return (Boolean)GetValue(SelectedDependencyProperty);
            }
            set
            {
                SetValue(SelectedDependencyProperty, value);
                if (value)
                {
                    Background = SelectedColor;
                }
                else
                {
                    Background = BackgroundColor;
                }
            }
        }
        public UIElementCollection Children
        {
            get { return (UIElementCollection) GetValue(ChildrenProperty.DependencyProperty); }
            private set { SetValue(ChildrenProperty, value); }
        }

        public ClickableControl()
        {
            InitializeComponent();
            Children = Grid.Children;
        }
//EXTRA CODE 
    }
}

XAML:

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:View"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:customUsercontrols="clr-namespace:View.custom_usercontrols"


//somewhere in the layout
<customUsercontrols:ClickableControl MouseDown="Search_OnMouseDown"
            GotFocus="Search_OnGotFocus"
            Background="#444444">
    <StackPanel Orientation="Horizontal">
        <materialDesign:PackIcon Kind="Magnify"     
                             Margin="25 0 0 0"           
                             Height="25" 
                             Width="25" 
                             Foreground="White" 
                             VerticalAlignment="Center"/>
        <customUsercontrols:HintTextBox x:Name="SearchT"
                                        Padding="15"
                                        Hint="SEARCH" 
                                        Width="204">
        </customUsercontrols:HintTextBox>
    </StackPanel>
</customUsercontrols:ClickableControl>

Thank you verry mutch

1

1 Answers

0
votes

This is a bit late, but for anyone that views this question and still wonder about it, here goes:

Don't inherit from UserControl(Which inherits from contentControl) and then change default Content property of it, and expect it's content to be recognized upon call to InitializeComponent();

The elements "inside" the UserControl are its Content. if you defer its content to another property, stuff will go haywire.

Either you put the control you want to name under the UserControl xaml definition(the usual way), or you add it in code behind and name it, or you can create a custom control and set its ControlTemplate with the control you want and specify it as a PART of the control:

http://paulstovell.com/blog/wpf-part-names