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

ItemsCollection that contains generic Visuals? (Canvas.ItemsSource

Closed Thread
 
Thread Tools Display Modes
Old 01-10-2006   #1 (permalink)
RyanLeeSchneider
Guest


 

ItemsCollection that contains generic Visuals? (Canvas.ItemsSource


I'm looking for a control that can auto-populate my canvas with databound
items.

Say I have a Canvas bound this this class:

public class Scene
{
private ObservableCollection<ISprite> _Actors = new
ObservableCollection<ISprite>();
public ObservableCollection<ISprite> Actors
{
get { return _Actors;}
}

public Scene()
{
_Actors.Add(new Sprite1());
_Actors.Add(new Sprite2());
}
}

public interface ISprite
{
double X {get;set;}
double Y {get;set;}
}

public class SpriteBase : ISprite
{
#region ISprite Members
...
#endregion

public SpriteBase(double X, double Y)
{
_X = X;
_Y = Y;
}
}

public class Sprite1 : SpriteBase
{
public Sprite1()
: base(10.0, 10.0)
{
}
}

public class Sprite2 : SpriteBase
{
public Sprite2()
: base(50.0, 50.0)
{
}
}


I want to use DataTemplates and something with an ItemsSource to display the
sprites in my canvas:

<Window.Resources>
<ObjectDataProvider x:Key="TheScene" ObjectType="{x:Type code:Scene}"/>
</Window.Resources>
<Canvas Name="MyCanvas" Width="{Binding Path=ActualWidth}"
Height="{Binding Path=ActualHeight}" DataContext="{StaticResource TheScene}">
<Canvas.Resources>
<DataTemplate DataType="{x:Type code:Sprite1}">
<Rectangle Canvas.Left="{Binding Path=X}" Canvas.Top="{Binding
Path=Y}" Height="10" Width="10" Fill="Red" />
</DataTemplate>
<DataTemplate DataType="{x:Type code:Sprite2}">
<Rectangle Canvas.Left="{Binding Path=X}" Canvas.Top="{Binding
Path=Y}" Height="10" Width="10" Fill="Blue" />
</DataTemplate>
</Canvas.Resources>
<ItemsControl ItemsSource="{Binding Path=Actors}" Height="200"
Width="200" Background="CornflowerBlue">
</ItemsControl>

</Canvas>


The above XAML just ends up with the Rectangles one after another in a list,
the Canvas.Top and Canvas.Left are ignored. Is there a better thing to bind
to?

Old 01-10-2006   #2 (permalink)
Drew Marsh
Guest


 

Re: ItemsCollection that contains generic Visuals? (Canvas.ItemsSource

RyanLeeScheider wrote:

> The above XAML just ends up with the Rectangles one after another in a
> list, the Canvas.Top and Canvas.Left are ignored. Is there a better
> thing to bind to?


This is a common mistake. The problem is that your the items themselves are
hosted in a container which is then put into the canvas, so setting the Canvas.Top/Left
on the datatemplate for the item doesn't make sense. Instead you must set
the ItemContainerStyle[1] property to a style that defines some kind of container
element and put the databound Canvas.Top/Left on it instead.

HTH,
Drew

[1] http://winfx.msdn.microsoft.com/libr...ainerStyle.asp

___________________________________
Drew Marsh
Chief Software Architect
Mimeo.com, Inc. - http://www.mimeo.com
Weblog - http://blog.hackedbrain.com/


Old 01-10-2006   #3 (permalink)
RyanLeeSchneider
Guest


 

Re: ItemsCollection that contains generic Visuals? (Canvas.ItemsSo


Thanks, Drew. Based on your comments I was able to find this link:

http://www.i-constructions.com/myblo...tabinding.html

Which is more or less exactly what I'm trying to do.

Ryan

"Drew Marsh" wrote:

> RyanLeeScheider wrote:
>
> > The above XAML just ends up with the Rectangles one after another in a
> > list, the Canvas.Top and Canvas.Left are ignored. Is there a better
> > thing to bind to?

>
> This is a common mistake. The problem is that your the items themselves are
> hosted in a container which is then put into the canvas, so setting the Canvas.Top/Left
> on the datatemplate for the item doesn't make sense. Instead you must set
> the ItemContainerStyle[1] property to a style that defines some kind of container
> element and put the databound Canvas.Top/Left on it instead.
>
> HTH,
> Drew
>
> [1] http://winfx.msdn.microsoft.com/libr...ainerStyle.asp
>
> ___________________________________
> Drew Marsh
> Chief Software Architect
> Mimeo.com, Inc. - http://www.mimeo.com
> Weblog - http://blog.hackedbrain.com/
>
>
>

Old 01-10-2006   #4 (permalink)
Drew Marsh
Guest


 

Re: ItemsCollection that contains generic Visuals? (Canvas.ItemsSo

RyanLeeSchneider wrote:

> Thanks, Drew. Based on your comments I was able to find this link:
>
> http://www.i-constructions.com/myblo...tabinding.html
>
> Which is more or less exactly what I'm trying to do.


Yup, as you can see you're not alone. The whole ItemsContainerStyle thing
is not immediately obvious, but makes total sense once you know and think
about it.

Happy coding,
Drew


Old 01-10-2006   #5 (permalink)
RyanLeeSchneider
Guest


 

Re: ItemsCollection that contains generic Visuals? (Canvas.ItemsSo


I've actually got a follow-up question, how can I apply a style (more
explicitly a DataTrigger in Style.Triggers) to the Items in the ItemsControl.
I tried adding a Style with Style.Triggers to the DataTemplate, and that
gives a runtime error (something annoyingly like DataTemplate.Triggers is
invalid child tag, only Triggers and Resources are allowed..), and adding it
to the Rectangle in my DataTemplate or in my parent Canvas or Windows all
lead to the triggers being ignored. My data classes all properly implement
INotifyPropertyChanged.

The DataTrigger SDK sample has the style in the same Resources block as the
DataTemplates, but this doesn't seem to be working for me. I have a feeling
the DataType for the trigger is the key...

Any thoughts?
Old 01-10-2006   #6 (permalink)
Drew Marsh
Guest


 

Re: ItemsCollection that contains generic Visuals? (Canvas.ItemsSo

RyanLeeScheider wrote:

> I've actually got a follow-up question, how can I apply a style (more
> explicitly a DataTrigger in Style.Triggers) to the Items in the
> ItemsControl.
> I tried adding a Style with Style.Triggers to the DataTemplate, and
> that
> gives a runtime error (something annoyingly like DataTemplate.Triggers
> is
> invalid child tag, only Triggers and Resources are allowed..), and
> adding it to the Rectangle in my DataTemplate or in my parent Canvas
> or Windows all lead to the triggers being ignored. My data classes
> all properly implement INotifyPropertyChanged.
>
> The DataTrigger SDK sample has the style in the same Resources block
> as the DataTemplates, but this doesn't seem to be working for me. I
> have a feeling the DataType for the trigger is the key...


This[1] works for me... or am I misunderstanding your problem?

HTH,
Drew

[1]
<Page xmlns="http://schemas.microsoft.com/winfx/avalon/2005" xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005"
>

<Grid>
<Grid.Resources>
<XmlDataProvider x:Key="data">
<root xmlns="">
<test value="1"/>
<test value="2"/>
<test value="3"/>
</root>
</XmlDataProvider>
</Grid.Resources>
<ItemsControl ItemsSource="{Binding Source={StaticResource data}, XPath=//test}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock x:Name="tb" Text="{Binding XPath=@value}"/>
<DataTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Trigger.Setters>
<Setter TargetName="tb" Property="FontWeight" Value="Bold"/>
<Setter TargetName="tb" Property="FontSize" Value="50"/>
</Trigger.Setters>
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Page>


Old 01-10-2006   #7 (permalink)
RyanLeeSchneider
Guest


 

Re: ItemsCollection that contains generic Visuals? (Canvas.ItemsSo


I'll have to try that, I didn't have my DataTemplates in
ItemsControl.ItemTemplate, but rather in Canvas.Resources in the parent.

However, I'm also trying to trigger on a change in the bound data object,
not a property of the visual.

For example, assume the data source was more like:

<test value="1" special="false"/>

And I change special to "true" in code, I want to have a trigger defined in
the XAML that changes the FontWeight to bold.

Would I be better off just using a value converter and do something like:

<TextBlock FontWeight={Binding
XPath=@special,Converter=MySpecialToFontWeightConverter} />

?


"Drew Marsh" wrote:


> <DataTemplate>
> <TextBlock x:Name="tb" Text="{Binding XPath=@value}"/>
> <DataTemplate.Triggers>
> <Trigger Property="IsMouseOver" Value="True">
> <Trigger.Setters>
> <Setter TargetName="tb" Property="FontWeight" Value="Bold"/>
> <Setter TargetName="tb" Property="FontSize" Value="50"/>
> </Trigger.Setters>
> </Trigger>
> </DataTemplate.Triggers>
> </DataTemplate>

Old 05-19-2006   #8 (permalink)
Attellati
Guest


 

Re: ItemsCollection that contains generic Visuals? (Canvas.ItemsSo

Hi Ryan,

I would try :

<test value = "0" special= "true"/<

Hahaha ... I don't know if it would work but it's worth giving it a go.

Cheers,

Attellati

--
Attellati


"RyanLeeSchneider" wrote:

>
> I'll have to try that, I didn't have my DataTemplates in
> ItemsControl.ItemTemplate, but rather in Canvas.Resources in the parent.
>
> However, I'm also trying to trigger on a change in the bound data object,
> not a property of the visual.
>
> For example, assume the data source was more like:
>
> <test value="1" special="false"/>
>
> And I change special to "true" in code, I want to have a trigger defined in
> the XAML that changes the FontWeight to bold.
>
> Would I be better off just using a value converter and do something like:
>
> <TextBlock FontWeight={Binding
> XPath=@special,Converter=MySpecialToFontWeightConverter} />
>
> ?
>
>
> "Drew Marsh" wrote:
>
>
> > <DataTemplate>
> > <TextBlock x:Name="tb" Text="{Binding XPath=@value}"/>
> > <DataTemplate.Triggers>
> > <Trigger Property="IsMouseOver" Value="True">
> > <Trigger.Setters>
> > <Setter TargetName="tb" Property="FontWeight" Value="Bold"/>
> > <Setter TargetName="tb" Property="FontSize" Value="50"/>
> > </Trigger.Setters>
> > </Trigger>
> > </DataTemplate.Triggers>
> > </DataTemplate>

Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Vista corrupt visuals Bill G. Vista General 0 05-19-2007 06:52 AM
Binding collection on ItemsSource - refresh problems =?Utf-8?B?QmVub2lzdC5EZW1ldXJl?= Avalon 0 07-21-2006 10:13 AM
visuals BuReNdE Vista performance & maintenance 0 06-16-2006 11:11 AM
Vista Visuals - What are they using? Ryan L. Masanz Vista General 4 04-30-2006 08:30 PM
Render Visuals in ASP.NET Willie Avalon 2 02-10-2006 01:21 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