![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
| |
| | #1 (permalink) |
| | 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? |
My System Specs![]() |
| | #2 (permalink) |
| | 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/ |
My System Specs![]() |
| | #3 (permalink) |
| | 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? |
My System Specs![]() |
| | #4 (permalink) |
| | 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> |
My System Specs![]() |
| | #5 (permalink) |
| | 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> |
My System Specs![]() |
| | #6 (permalink) |
| | 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> |
My System Specs![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| binding to combobox itemssource? | .NET General | |||
| visuals | Vista performance & maintenance | |||
| Vista Visuals - What are they using? | Vista General | |||