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 - Custom Data Binding with ItemsControl ControlTemplate Problem

 
 
Old 06-02-2006   #1 (permalink)
HolaMan


 
 

Custom Data Binding with ItemsControl ControlTemplate Problem

Hi~~ I encounter a problem.

I want to use ItemsControl and Binding my list data to ItemsSource
such as
<ListBox ItemsSource={Binding myData...etc}/>

then define DataTempalte for myData
<DataTemplate TargetType={x:Type local.myData}>
<Rectangle Fill="Red"/>
</DataTempalte>

And I define a new Template for ListBox
<Style TargetType="{x:Type ListBox}">
....
<ControlTemplate>
<local:MyPanel IsItemsHost="true"/>
</ControlTempalte>
</Style>

I want to layout rectangles( represent mydata by DataTemplate) on
MyPanel
So I override ArrangeOverride Function and write code below

foreach( UIElement u in Children)
.....etc

But I find that I get all "ContentPresenter", not Rectangles.
If there's any way to get real controls from MyPanel?

Thanks


My System SpecsSystem Spec
Old 06-02-2006   #2 (permalink)
Marcus


 
 

Re: Custom Data Binding with ItemsControl ControlTemplate Problem

The fact that you are getting ContentPresenter is extremelly normal and
corresponds to the actual design of the ItemsControl...

Each item in the ItemsControl is represented by an Item Container, now
the Item Container Template MUST contain a ContentPresenter, since it
is the ContentPresenter that will be the control in charge of
displaying your data.

To obtain you rectangle object, what you must do is get the first child
of the ContentPresenter... The ContentPresenter's child will be the
root element of the data template used to display your data.

To get the child of the ContentPresenter, you can use
VisualTreeHelper.GetChild(u, 0)




HolaMan wrote:
> Hi~~ I encounter a problem.
>
> I want to use ItemsControl and Binding my list data to ItemsSource
> such as
> <ListBox ItemsSource={Binding myData...etc}/>
>
> then define DataTempalte for myData
> <DataTemplate TargetType={x:Type local.myData}>
> <Rectangle Fill="Red"/>
> </DataTempalte>
>
> And I define a new Template for ListBox
> <Style TargetType="{x:Type ListBox}">
> ...
> <ControlTemplate>
> <local:MyPanel IsItemsHost="true"/>
> </ControlTempalte>
> </Style>
>
> I want to layout rectangles( represent mydata by DataTemplate) on
> MyPanel
> So I override ArrangeOverride Function and write code below
>
> foreach( UIElement u in Children)
> .....etc
>
> But I find that I get all "ContentPresenter", not Rectangles.
> If there's any way to get real controls from MyPanel?
>
> Thanks


My System SpecsSystem Spec
Old 06-07-2006   #3 (permalink)
HolaMan


 
 

Re: Custom Data Binding with ItemsControl ControlTemplate Problem

Dear Marcus
I got it...
Thanks you very much :-)

My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
wpf xml twoway data binding problem? .NET General


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