I currently try to create classes for a paint-like WPF application. I have to base classes LineMovement (line from StartPoint to EndPoint) and PathMovement (line going through all points specified in a property Points of type PointCollection). These classes inherit from Control and get their looks through a ControlTemplate.
Now I want to add (what I call) PointMovers to the ControlTemplate. These should be little visual elements residing on each of the points in either of the Movement classes. They should become a kind of grip mechanism to drag the underlying point around.
The problem is of course that I don't know a way to create a variable number of elements in a ControlTemplate. It would be cool if I could do something like this:
<Style x:Key="{x:Type mov:PathMovement}" TargetType="{x:Type mov:PathMovement}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type mov:PathMovement}">
<Grid>
<Polyline Points="{TemplateBinding Points}" />
<!-- interesting part start -->
<foreach loopvariable="Point" in="{TemplateBinding Points}">
<PointMover Point="Point" />
</foreach>
<!-- interesting part end -->
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Is this possible? Do you have another approach in mind that could work?
Thanks in advance!
Use an ItemsControl in conjunction with an ItemTemplate:
<ItemsControl ItemsSource="{Binding Points}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<!-- rendered for each point -->
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
By the sounds of your use case, you may also want to read up on AdornerLayers.
Related
I'm looking at using the Silverlight Menu Control from Codeplex. It's functionally very good but I'd like to restyle the appearance. Is it possible to do this without just making a new copy of the template and changing a few properties? Here's an example from the generic "theme" that comes with the control:
<Style TargetType="GenericControl:MenuBar">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="GenericControl:MenuBar">
<Canvas x:Name="LayoutRootMB">
<StackPanel Height="25"
x:Name="baseRectMB"
Canvas.Left="0"
Canvas.Top="0"
HorizontalAlignment="Left"
Orientation="Horizontal" />
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I'd like to change, say, HorizontalAlignment to Stretch (for the sake of argument -- never mind if it'll actually accomplish anything useful). Is there any way to tell XAML (in XAML, not in C#) that I want to do something to baseRectMB?
baseRectMB is named in a TemplatePartAttribute on class MenuBar.
I assume I can simply copy the existing template and alter it, though I haven't gotten that working yet either.
I've searched around quite a bit and can't seem to crack this nut.
I've got an app with a main view that changes dynamically, and to do this I use content presenter with a binding to a control:
<ScrollViewer Grid.Column="2" x:Name="StepScrollViewer">
<StackPanel Margin="20,20,20,500">
<ContentPresenter Content="{Binding MainControl}"/>
</StackPanel>
</ScrollViewer>
Then I change the MainControl at runtime in my view model. The problem is that the controls getting bound don't reliably display their error templates... I suspect it is for the reasons discussed here:
Validation ErrorTemplate not showing on data errors
But the fix for this problem doesn't seem to work for me because I'm not using a control template around my content presenter. When I wrap an AdornmentDecorator tag around my content presenter, it doesn't seem to fix the problem. It DOES work if I put an AdornmentDecorator inside each control I load into the contentpresenter (as the root element), but I'd like to avoid this repetition if possible.
Any insights?
UPDATE
I tried this approach suggested by Dennis, but to no avail. The control binds okay, but it works no better than the current approach (also shown commented below). Note: I tried it both with the AdornerDecorator as a singleton element the way Dennis has it, and surrounding the ContentPresenter, as shown below. Neither show any difference - the adorners around my controls all disappear when the MainControl binding is changed.
<UserControl.Resources>
<Style x:Key="MainContentControl" TargetType="{x:Type ContentControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContentControl}">
<Grid>
<AdornerDecorator>
<ContentPresenter Content="{Binding MainControl}"/>
</AdornerDecorator>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid>
.....
<ScrollViewer Grid.Column="2" x:Name="StepScrollViewer">
<StackPanel Margin="20,20,20,500" >
<ContentControl Style="{StaticResource MainContentControl}"/>
</StackPanel>
</ScrollViewer>
<!-- THE BELOW WORKS IF I SURROUND EACH BOUND CONTROL WITH adornerdecorator -->
<ScrollViewer Grid.Column="2" x:Name="StepScrollViewer">
<StackPanel Margin="20,20,20,500">
<ContentPresenter Content="{Binding MainControl}"/>
</StackPanel>
</ScrollViewer>
-->
Instead of using a ContentPresenter directly, I would instead use a ContentControl. A ContentControl is the base class for controls that contain other elements and have a Content property, e.g. Button.
Then you can override the template to have an AdornerDecorator next to the ContentControl. This is different to what you previously tried as now the ContentPresenter is part of the same visual tree as the Adorner.
<Style TargetType="{x:Type ContentControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContentControl}">
<AdornerDecorator>
<ContentPresenter/>
</AdornerDecorator>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Edit: Forgot that the AdornerDecorator needs to wrap the container, not just sit side-by-side.
I am currently working with avalon dock v2, in the template of my document sources, i'm also putting in a docking manager.
Yes for each of my document, I want anchorable panes inside it. But when I try to do that, it doesn't work, it just shows the toString of the docking manager for each of the document, is there a way to fix that.
Also, how do i default dock my anchorable?
Thanks and Regards,
Kev84
In creating a template for the AvalonDock's LayoutDocument (via the LayoutDocumentControl) I also came across a similar issue. The solution was to set the ContentSource of the ContentPresenter to point to the Model property of my control. The code below illustrates it:
<!--The LayoutDocument is templated via the LayoutDocumentControl-->
<Style TargetType="{x:Type ad:LayoutDocumentControl}">
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ad:LayoutDocumentControl}">
<ScrollViewer
Background="AliceBlue"
HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" SnapsToDevicePixels="True">
<!--Make sure that the ContentSource points the Model Property of the Control-->
<ContentPresenter
Content="{Binding Path=Content, UpdateSourceTrigger=PropertyChanged}"
ContentSource="{Binding Path=Model, UpdateSourceTrigger=PropertyChanged}"
/>
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
A similar approach should apply to your case. This is just a temptative answer (since I am also new to AvalonDock 2.0), but it may be worth trying.
Live long and prosper!
Is there a way in Silverlight 4 to dictate that all elements within a StackPanel must have a margin, instead of specifying margin="10,0" on each one?
I'm afraid it's not possible declaratively in XAML with the StackPanel directly. It's the conceptual philosophy in Silverlight/WPF that a panel should not modify properties of its children. So you could implement your own Panel that does so anyway, or you could use an ItemsControl like that:
<ItemsControl>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentPresenter Margin="10,0" Content="{Binding Content}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
[...]
</ItemsControl>
An ItemsControl uses a StackPanel by default, you can use its ItemsPanel property to define another Panel as an ItemsPanelTemplate if you wish so.
The way I'd do it is by defining implicit styles in the StackPanel's resources, for each control type that will be used within the StackPanel. To save defining the value repeatedly for each control type, you can create a named base style that targets FrameworkElement and defines the style(s), from which the style for each control type can inherit. An example is below:
<StackPanel Orientation="Horizontal">
<StackPanel.Resources>
<Style x:Key="CommonStyle" TargetType="FrameworkElement">
<Setter Property="Margin" Value="10,0" />
</Style>
<Style TargetType="Button" BasedOn="{StaticResource CommonStyle}" />
<Style TargetType="TextBlock" BasedOn="{StaticResource CommonStyle}" />
<Style TargetType="CheckBox" BasedOn="{StaticResource CommonStyle}" />
</StackPanel.Resources>
<Button>Button</Button>
<TextBlock Text="Text" />
<CheckBox>Check Box</CheckBox>
</StackPanel>
Note how each control in the StackPanel will have the margin applied, without needing to define it on each control.
Hope this helps...
Chris Anderson
PS. Blatant self promotion - this is based upon the inheritance trick in my book Pro Business Applications with Silverlight 4 :).
Put your stackpanel within a Border element and set the Border Padding to "10 0"
You can also do this programmatically; your StackPanel has a Children collection. You could use this to iterate through them and set the margin.
I'm customising the appearance of grouping in a ListBox. In ListBox.Resources, I have declared something like (formatting removed):
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<StackPanel Orientation="Vertical">
<!-- Group label -->
<ContentPresenter />
<!-- Items in group -->
<ItemsPresenter />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The actual group label is not very readable and I'd like to use a value converter to make it more presentable. However I cannot find a way to obtain this text and convert it.
I figure that a Binding would let me use a converter.
I've tried replacing the ContentPresenter above with the likes of...
<TextBlock Text="{TemplateBinding Content}"/>
<TextBlock Text="{Binding}"/>
...and numerous other things, but to no avail. Any suggestions?
Well isn't that just typical. I found the answer shortly after posting...
<TextBlock Text="{Binding Path=Content.Name,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=GroupItem},
Converter={StaticResource MyConverter}}"/>
Sometimes just the process of actually asking the question draws the answer out of thin air. In this case looking at the source code of GroupItem in .NET Reflector did the trick.
Hope someone else finds this edge case useful. Still, it would be a lot nicer if GroupItem exposed a property for this directly.
I'll still award a correct answer to anyone who knows a nicer way of doing this.