Reputation: 294
My English skill is poor because I'm not a native English speaker.
I have created two custom control. First control has a DP named CaretIndexFromLine and the control name is TextArea. The following is the summarized cs code of the first control.
public class TextArea : TextBox
{
public int CaretIndexFromLine
{
get { return (int)GetValue(CaretIndexFromLineProperty ); }
set { SetValue(CaretIndexFromLineProperty , value); }
}
public static readonly DependencyProperty CaretIndexFromLineProperty =
DependencyProperty.Register("CaretIndexFromLine", typeof(int), typeof(TextArea), new P
ropertyMetadata(0));
...
}
The second control named Editor has child control TextArea.
The following is the summarized XAML code of the second control.
<Editor> <TextArea x:Name="PART_TextArea"/> </Editor>
And the following is summarized cs code of the second control.
public class Editor : Control
{
public TextArea TextArea { get; private set; }
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.TextArea = (TextArea)Template.FindName("PART_TextArea", this);
}
...
}
And I was going to use the above control in the MainWindow.
<MainWindow> <Editor/> </MainWindow>
There is a problem here. I want to access the property (CaretIndex) of the TextArea in the XAML code. I tried like this but It was not operated.
<MainWindow>
<StackPanel>
<Editor x:Name="editor"/>
<TextBlock Text="{Binding ElementName=editor.TextArea, Path=CaretIndexFromLine}/>
</StackPanel>
</MainWindow>
What should I do to achieve the above goal?
Thank you for reading this and please teach me the solution.
Upvotes: 1
Views: 67
Reputation: 169150
Your Editor
control has an TextArea
property so just set the ElementName
to "editor" and the Path
to TextArea.CaretIndexFromLine
:
<TextBlock Text="{Binding ElementName=editor, Path=TextArea.CaretIndexFromLine}/>
TextArea
should be a read-only dependency property though in order for a notification to be raised when it's set in the OnApplyTemplate()
method.
Upvotes: 2