![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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? |
| | #2 (permalink) |
| 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/ |
| | #3 (permalink) |
| 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/ > > > |
| | #4 (permalink) |
| 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 |
| | #5 (permalink) |
| 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? |
| | #6 (permalink) |
| 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> |
| | #7 (permalink) |
| 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> |
| | #8 (permalink) |
| 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> |
| |
| |
![]() |
| 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 |