sergun

<Window ...>

<Window.Resources>
DataTemplate x:Key="myTemplate" DataType="{x:Type ...}">
<TextBox x:Name="t"/>
</DataTemplate>
</Window.Resources>

...
<ContentControl x:Name="cc" ContentTemplate="myTemplate">

</Window>


How can I refer to given in myTemplate TextBox from
some event handler of Window



Re: Windows Presentation Foundation (WPF) How to refer root control of DataTemplate when it is applied to ContentCOntrol?

Yi-Lun Luo - MSFT

You first need to get the applied template, by getting the Template property value of the control where the template is applied. Then, you call the template version of FindName, passing the control where the template was applied as the second parameter.






Re: Windows Presentation Foundation (WPF) How to refer root control of DataTemplate when it is applied to ContentCOntrol?

xpwang_leo

Yi-Lun is right. Like this

TextBox textTemplate = (TextBox)cc.Template.FindName("t", cc);

Leo






Re: Windows Presentation Foundation (WPF) How to refer root control of DataTemplate when it is applied to ContentCOntrol?

George F DArcy

xpwang_leo wrote:

Yi-Lun is right. Like this

TextBox textTemplate = (TextBox)cc.Template.FindName("t", cc);

Leo



Hi, I don't mean to hi-jack this thread, but I am facing a similar problem.

I have a ContentControl;
<ContentControl Name="cc" ContentTemplate="{StaticResource detailTemplate}" />

and "detailTemplate" is a DataTemplate;

<DataTemplate x:Key="detailTemplate">
.
.
.
<TextBox x:Name="MyTextBox" Text="{Binding Path=Lines, Mode=TwoWay, Converter={StaticResource IdentityConverter}}" />
.
.
.
</DataTemplate>

I try to get access to the TextBox, so that I can give it focus, and this always returns null;

TextBox temp = (TextBox)cc.Template.FindName("MyTextBox", cc);


Any help would be appreciated (and will hopefully help the OP)

thanks








Re: Windows Presentation Foundation (WPF) How to refer root control of DataTemplate when it is applied to ContentCOntrol?

George F DArcy

I posts the above message yesterday, but for some reason it didnt 'bump' this thread. So I'll try again! Smile




Re: Windows Presentation Foundation (WPF) How to refer root control of DataTemplate when it is applied to ContentCOntrol?

xpwang_leo

Hi, George

cc.Template can get the ControlTemple included in the style. So here you can't use it.

cc.ContentTemplate can just get the DataTemplate.

TextBox txt = (TextBox)cc.ContentTemplate.FindName("MyTextBox", cc);

But I got a exception : {"This aoperation is valid only on elements that have this template pplied."}.

I found the DataTemplate.FindName() method used the second argument incorrectly.

Now I have no any idea about it.

So sorry !






Re: Windows Presentation Foundation (WPF) How to refer root control of DataTemplate when it is applied to ContentCOntrol?

sergun

I have the same result Sad



Re: Windows Presentation Foundation (WPF) How to refer root control of DataTemplate when it is applied to ContentCOntrol?

Yi-Lun Luo - MSFT

The template is applied to the ContentControl’s ContentPresenter, not the ContentControl itself. So you should pass the ContentPresenter as the second parameter. For example:

<ContentControl>

<ContentPresenter Name="cp" ContentTemplate="{StaticResource myTemplate}" / >

</ContentControl>

TextBox textName = cp.ContentTemplate.FindName("textName", cp) as TextBox;






Re: Windows Presentation Foundation (WPF) How to refer root control of DataTemplate when it is applied to ContentCOntrol?

sergun

He-he Smile

I found the same solution and just started posting him.

Thanks!





Re: Windows Presentation Foundation (WPF) How to refer root control of DataTemplate when it is applied to ContentCOntrol?

xpwang_leo

How to get "cp"




Re: Windows Presentation Foundation (WPF) How to refer root control of DataTemplate when it is applied to ContentCOntrol?

Yi-Lun Luo - MSFT

Here cp is the Name of the ContentPresenter. So you can use it directly in the code.