WPF无法在Usercontrol

时间:2016-10-23 16:49:06

标签: c# wpf xaml user-controls

我正在创建一个WPF程序,我创建了一个自定义Usercontrol和自定义文本框

当我在visual studio中重建我的解决方案时,我得到了这个错误。

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

我不知道自己需要做什么。或者我做错了什么?有人能帮我吗?下面的类是usercontrol和hinttextbox,最后一个是我在xaml中实现它们的方式。

这就是我将文本框放入Usercontrol

的方法

TEXTBOX = HintTextBox

命名空间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
 }

}

这是我的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>

谢谢verry mutch

1 个答案:

答案 0 :(得分:0)

这有点晚了,但是对于那些看到这个问题但仍然对此感到疑惑的人来说,这里有:

不要从UserControl继承(继承自contentControl),然后更改它的默认Content属性,并期望在调用InitializeComponent()时识别它的内容;

UserControl中的“内部”元素是其内容。如果你将其内容推迟到另一个属性,那么东西就会变得混乱。

您可以在UserControl xaml定义(通常的方式)下放置您想要命名的控件,或者将其添加到代码后面并命名, 或者您可以创建自定义控件并使用您想要的控件设置其ControlTemplate,并将其指定为控件的一部分:

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