2016-10-23 7 views
0

にカスタムテキストボックスの名前を設定することはできません。WPFは、私はWPFのプログラムを作成していると私は、カスタムユーザーコントロールとカスタムテキストボックス</p> <p>私は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 { 公共部分クラスのHintTextBox:TextBoxの { のpublic static読み取り専用のDependencyProperty HintepDependencyProperty = DependencyProperty.Register( "ヒント"、typeof(文字列)、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

答えて

0

これは少し遅れているが、この質問を見て、まだそれについて疑問に思う人のために、ここに行く:

は(ContentControlにから継承)UserControlから継承し、それをデフォルトのContentプロパティを変更しないでください、 InitializeComponent()の呼び出し時にコンテンツが認識されることを期待してください。

UserControlの「内部」の要素は、そのコンテンツです。そのコンテンツを別のプロパティに委譲すると、物事はうんざりになります。

UserControl xaml定義(通常の方法)に名前を付けるコントロールを追加するか、コードの後ろにコードを追加して名前を にするか、カスタムコントロールを作成してそのコントロールでControlTemplateを設定しますコントロールのPARTとして指定する必要があります。

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

関連する問題

 関連する問題