Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - FrameworkTemplate.LoadContent vs bindings

 
 
Old 03-23-2006   #1 (permalink)
Pascal Bourque


 
 

FrameworkTemplate.LoadContent vs bindings

Hi,

I have a ControlTemplate that I want to use to generate Control
instances. I've managed to do it successfully with the LoadContent() method:

Control c = ( Control )controlTemplate.LoadContent();

The only problem with this is that the bindings that are defined in my
ControlTemplate don't seem to be propagated to the generated Control
instance...

Is there another way of creating a Control from a ControlTemplate that
would work with bindings? Or is there a way to transfer the bindings
manually after LoadContent() has been called?

Thanks!

Pascal

My System SpecsSystem Spec
Old 03-24-2006   #2 (permalink)
Nick Kramer [MSFT]


 
 

Re: FrameworkTemplate.LoadContent vs bindings

> The only problem with this is that the bindings that are defined in my
> ControlTemplate don't seem to be propagated to the generated Control
> instance...


Can you elaborate? Thanks.

--
-Nick Kramer [MSFT]

---
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

"Pascal Bourque" <bourquep@xceedsoft.com> wrote in message
news:eVXgnJsTGHA.1160@TK2MSFTNGP09.phx.gbl...
> Hi,
>
> I have a ControlTemplate that I want to use to generate Control instances.
> I've managed to do it successfully with the LoadContent() method:
>
> Control c = ( Control )controlTemplate.LoadContent();
>
> The only problem with this is that the bindings that are defined in my
> ControlTemplate don't seem to be propagated to the generated Control
> instance...
>
> Is there another way of creating a Control from a ControlTemplate that
> would work with bindings? Or is there a way to transfer the bindings
> manually after LoadContent() has been called?
>
> Thanks!
>
> Pascal



My System SpecsSystem Spec
Old 03-27-2006   #3 (permalink)
Pascal Bourque


 
 

Re: FrameworkTemplate.LoadContent vs bindings

Here's a simple repro. When you click on the button, I locate the
ControlTemplate, generate a Control from this template (via
LoadContent), set its DataContext and add it to the StackPanel. The
binding declared in the ControlTemplate is not propagated to the Control
instance.

XAML:

<Window x:Class="ControlTemplateBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ControlTemplateBinding"
>

<Window.Resources>
<ControlTemplate x:Key="myTemplate">
<TextBox Text="{Binding
Path=Text,UpdateSourceTrigger=PropertyChanged}" />
</ControlTemplate>
</Window.Resources>
<StackPanel x:Name="stackPanel">
<TextBlock x:Name="dataSource"
Text="This TextBlock.Text is bound to the following
TextBox.Text" />
<TextBox DataContext="{Binding ElementName=dataSource}"
Text="{Binding
Path=Text,UpdateSourceTrigger=PropertyChanged}" />
<Button Click="buttonClicked"
Content="Click here to create a TextBox from a
ControlTemplate" />
</StackPanel>
</Window>


CODE BEHIND:

private void buttonClicked( object sender, RoutedEventArgs e )
{
ControlTemplate template = ( ControlTemplate )this.FindResource(
"myTemplate" );
Control control = ( Control )template.LoadContent();
control.DataContext = this.dataSource;
this.stackPanel.Children.Add( control );
}


Hope this helps clarify what I'm trying to do...

Thanks!

Pascal


Nick Kramer [MSFT] wrote:
>> The only problem with this is that the bindings that are defined in my
>> ControlTemplate don't seem to be propagated to the generated Control
>> instance...

>
> Can you elaborate? Thanks.
>



My System SpecsSystem Spec
Old 03-27-2006   #4 (permalink)
Marcus


 
 

Re: FrameworkTemplate.LoadContent vs bindings

Hi,

I noticed an extremelly similar behavior to what you observed...
However, in my case, its the Markup Expression "{x:Static expr }" which
seems not to be propagated...

In my case, I want to generate UI Elements based on a template from
within a custom control...

I created a DP to hold the "ControlTemplate" for those elements and set
the Control Template from within the XAML using my custom control...

I want my custom control to generate the required UI Elements during
the layout phase so in my MeasureOverride(), I create the appropriate
UIElements using the same:

UIElement _element = SeparatorTemplate.LoadContent();

where SeparatorTemplate is my CLR accessor for the
ControlTemplate's DP...

This generates the tree for the UIElement just fine, however, the UI
Element which text was set using the x:Static markup expression is
blank...

The markup expression is valid since I can use the same in different
context just fine...

Any ideas or suggestions?

My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
Question on parameter bindings PowerShell
Key Bindings Changed General Discussion
Unix-like key bindings? PowerShell
Adapters and Bindings in Vista beta 2 Vista networking & sharing


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46