2016-10-23 2 views
0

나는 WPF 프로그램을 만드는거야 사용자 지정 텍스트 상자의 이름을 설정할 수 없습니다와 나는WPF는 UserControl을

나는이 오류가 Visual Studio에서 내 솔루션을 다시 정의의 UserControl 및 사용자 정의 텍스트 상자를 만들었습니다.

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에서 어떻게 구현했는지입니다.

네임 스페이스 View.custom_usercontrols { 공공 부분 클래스 HintTextBox : 텍스트 상자 { 공공 정적 읽기 전용 DependencyProperty에 HintepDependencyProperty =

내가 내 UserControl을

텍스트 상자 = HintTextBox에 텍스트 상자를 넣어하는 방법이다 DependencyProperty.Register ("Hint", typeof (string), typeof (HintTextBox));

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 모자의 일종

:

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입니다 ,

는 UserControl에서 상속 (ContentControl에서 상속)하고 그것의 기본 Content 속성을 변경하지 마십시오 :

답변

0

이 조금 늦게,하지만이 질문을 볼 아직도 그것에 대해 궁금해 누군가를 위해, 여기 간다 InitializeComponent()에 대한 호출시 인식 될 내용을 기대합니다.

"내부"요소는 UserControl입니다. 그 콘텐츠를 다른 속성으로 연기하면, 물건이 엉망이 될 것입니다.

UserControl xaml 정의에 이름을 지정하려는 컨트롤 (일반적인 방법)을 넣거나 코드 뒤에 코드를 추가하고 이름을 으로 지정하거나 사용자 지정 컨트롤을 만들고 컨트롤과 함께 ControlTemplate을 설정할 수 있습니다. 컨트롤의 일부로 지정하고 컨트롤의 일부로 지정하십시오.

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