2010-04-14 6 views
1

"BASE"라는 Silverlight 사용자 지정 컨트롤이 있습니다. 이 클래스에서 상속받은 또 다른 컨트롤 인 "CHILD1"이 있습니다. BASE에는 CHILD1 컨트롤의 내용을 보유하는 ContentPresenter가 있습니다. CHILD1 컨트롤의 내용에있는 TextBox에 액세스해야합니다. 즉, 초기화되고 표시되지만 코드에서 항상 null입니다.Silverlight 3 컨트롤을 상속하면 내용이 초기화되지 않습니다.

콘텐츠 컨트롤의 children 컬렉션을 반복하는 대신 이러한 컨트롤에 직접 액세스 할 수 있습니까?

감사합니다.

자식 1 :

<ContentPresenter Grid.Row="1" 
         x:Name="cprContent" 
         Content="" /> 

기본 클래스 코드 : BASE의

<local:BASE x:Class="CWTest1.CHILD1" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:local="clr-namespace:CWTest" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      Width="400" 
      Height="300"> 
<Grid x:Name="LayoutRoot2" 
     Background="White"> 
    <TextBox x:Name="tbx1" 
      Text="xx" /> 
</Grid> 

public partial class CHILD1 : BASE 
{ 
    public CHILD1() 
    { 
     InitializeComponent(); 

     // this.tbx1 is always null 
     this.tbx1.Focus(); 
    } 
} 

부 -

[ContentProperty("Content")] 
public partial class cwBase1 : ChildWindow 
... 
new public object Content 
    { 
     get { return cprContent.Content; } 
     set { cprContent.Content = value; } 
    } 
+0

BASE에 대한 자세한 내용은 무엇입니까? 그것은'ContentControl'인가 아니면 일반적인'Control'인가? – AnthonyWJones

+0

는 "ChildWindow" [ContentProperty ("콘텐츠")] 공공 부분 클래스 cwBase1에서 파생 : ChildWindow ... 새 공용 객체 내용 { GET은 {cprContent.Content를 반환; } {cprContent.Content = value; }} – Sako73

+0

서식이 부족하여 죄송합니다. 의견에 linebreaks가 표시되지 않습니다. – Sako73

답변

0

생성자 대신 OnApplyTemplate 재정의에 포커스를 맞추려고하면 TextBox가 여전히 null입니까?

public partial class CHILD1 : BASE 
{ 
    public CHILD1() 
    { 
     InitializeComponent(); 
    } 

    public override void OnApplyTemplate() 
    { 
     base.OnApplyTemplate(); 
     this.tbx1.Focus(); 
    } 
} 
+0

예, 여전히 null입니다. – Sako73

0
내가 여기이 어렵지만해야한다고 생각하지 않습니다

는 내가 가진 그 주변의 작업이 작동하는 것입니다

[ContentProperty("Content2")] 
public partial class cwBase1 : ChildWindow 
{ 
    .... 
    public object Content2 
    { 
     get { return cprContent.Content; } 
     set { cprContent.Content = value; } 
    } 
    .... 
    protected T GetUIElement<T>(string name) 
    { 
     UIElement el = ((Grid)this.Content2).Children.FirstOrDefault(ui => 
      ui.GetType() == typeof(T) && 
      ui.GetType().GetProperty("Name").GetValue(ui, null).ToString() == name); 

     return (T)(object)el; 
    } 
} 




public partial class inherit2 : cwBase1 
{ 
    public inherit2() 
    { 
     InitializeComponent(); 
     GetUIElement<TextBox>("tbx1").Focus(); 
    } 
} 

나는 아직도 기술적으로 올바른 해결책이 무엇인지 진지하게 경청입니다 .

관련 문제