Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > Avalon

FrameworkTemplate.LoadContent vs bindings

Closed Thread
 
Thread Tools Display Modes
Old 03-23-2006   #1 (permalink)
Pascal Bourque
Guest


 

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
Old 03-24-2006   #2 (permalink)
Nick Kramer [MSFT]
Guest


 

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



Old 03-27-2006   #3 (permalink)
Pascal Bourque
Guest


 

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.
>



Old 03-27-2006   #4 (permalink)
Marcus
Guest


 

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?

Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bindings to online: how to change them with network conditions Craig Kelly Avalon 0 03-06-2007 02:31 PM
Unix-like key bindings? =?Utf-8?B?cm5pa2FuZGVy?= PowerShell 1 08-27-2006 09:54 PM
Adapters and Bindings in Vista beta 2 scott Vista networking & sharing 3 05-29-2006 11:07 AM








Vistax64.com 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 2005-2008

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 47 48 49 50