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 - Nexting control templates

 
 
Old 05-25-2006   #1 (permalink)
Griff


 
 

Nexting control templates

I have a subclass of Button (myButton)

It has dep props ButtonText and ButtonIcon
(string and object)

I want to use this is many different style situations.
I have a number of different "button background" scenarios, and also a
number of different ways I'd like to lay out the test and icon.

So I might write in my main markup.

<local:MyButton Style="MyStyle23"
ButtonText="PushMe"
ButtonIcon="{StaticResource Icon23}" />

and in a resource dictionary somewhere I'd like to write something like this

<Style x:Key="CaderaButtonStyle" TargetType="Button">
<Style.Triggers>
<Trigger Property="Button.IsPressed"
Value="true">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid>
<Control Grid.Row="0"
Template="{StaticResource ButtonBaseDesign12}"/>
<Control Grid.Row="0"
Template="{StaticResource
ButtonContentLayout34}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>


that is to say, I define a load of styles which mix and match backgrounds
and content layouts.

the problem is how I display the content.

If I have a control template (called say ButtonContentLayout1) which defines
a grid and sticks some text in one place and an icon somewhere else, how do
I access the content ?

As far as the style is concerned, I have two bits of information called
ButtonText and ButtonContent (the style is of target type MyButton) but my
ButtonContentLayout1 Control Template has to be target Type control if I'm to
use it in my style, which seems to mean that it cannot contain statements like

<TextBlock Text="{TemplateBinding Property=ButtonText}"/>

Because parameters of that name don't exist in a control.

Can I define properties (like variables) in my ButtonContentLayout control
template (say param1, param2) and then
- use them in the control template
- set them from the Style ?

or is there another way I can do this ?

Thanks in advance.

Template
--
Griff
(trying to make an industrial UI with XAML/WPF/c#)

My System SpecsSystem Spec
Old 05-26-2006   #2 (permalink)
lee


 
 

Re:Nexting control templates

following is an example on how you can do this
<Window x:Class="winyah.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="winyah" Height="600" Width="600"
xmlns:local="clr-namespace:winyah" Name="window" Background="Aquamarine"
>

<Window.Resources>

<local:ImageConverter1 x:Key="qa2"></local:ImageConverter1>
<ControlTemplate x:Key="st1" TargetType="{x:Type local:MyButton}">
<Border Width="120" Height="50" BorderBrush="black" BorderThickness="2">
<Grid Height="50" ShowGridLines="true" Width="100">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Source="{Binding RelativeSource={RelativeSource
TemplatedParent}, Path=ButtonIcon, Converter={StaticResource qa2} }">
</Image>
<TextBlock Grid.Column="1" Text="{TemplateBinding ButtonText}"/>
</Grid>
</Border>
</ControlTemplate>
</Window.Resources>
<StackPanel>
<local:MyButton ButtonIcon="a1.gif" ButtonText="a button"
Template="{StaticResource st1}">

</local:MyButton>
<local:MyButton ButtonIcon="a2.gif" ButtonText="another 1"
Template="{StaticResource st1}">

</local:MyButton>
</StackPanel>

</Window>

public class MyButton:Button{
public static DependencyProperty ButtonIconProperty =
DependencyProperty.Register(
"ButtonIcon",
typeof(string), typeof(MyButton)
);
public string ButtonIcon
{
get { return (string)GetValue(ButtonIconProperty); }
set { SetValue(ButtonIconProperty, value); }
}
public static DependencyProperty ButtonTextProperty =
DependencyProperty.Register(
"ButtonText",
typeof(string), typeof(MyButton)
);
public string ButtonText
{
get { return (string)GetValue(ButtonTextProperty); }
set { SetValue(ButtonTextProperty, value); }
}


}
public class ImageConverter1 : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
BitmapImage myBitmapImage = new BitmapImage();


myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri(value.ToString(), UriKind.
RelativeOrAbsolute);

myBitmapImage.DecodePixelWidth = 200;
myBitmapImage.EndInit();

return myBitmapImage;

}

public object ConvertBack(object value, Type targetType, object
parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
My System SpecsSystem Spec
Old 05-31-2006   #3 (permalink)
Griff


 
 

Re:Nexting control templates

Lee, thanks.

This doesn't actually solve my problem, which I maybe need to explain
better. (But thanks for the intro to converters, which has been a great help
for another problem I had)


I have no problem creating a control template to use ButtonText and
ButtonIcon info specified when I deploy MyButton. What I need to do is a bit
more messy.

I have a style (with triggers) that allows me to choose from 1 of 4
different templates according to the state of the button (whether it be up,
down, stuck down (like a radio button that is selected) or just grayed out if
not enabled.
That's all fine.

But I have two different aspects to the style of a button
- it's shape & background (ie a round grey button or a blue rectangular
button)
- the content layout (ie text above icon, text below icon, text to left of
icon)

And I use these in a whole manner of permutations.

If I just created templates for every possible permutation I'd end up with a
huge pile of templates with a lot of duplication so I want to be a bit
cleverer.

So I'd like either
- a template for a particular button that calls up two other templates (one
for appearance, one layout)
or
- a way of combining templates for appearance and layout into the relevant
trigger section of the final style

taking the first approach, the problem comes when I try and pass parameters
into the layout template.
Because I'm deploying the layout template into the final template, the
target type of the layout template is "control".
I then cannot reference dependency properties from the original target
(MyButton) in TemplateBindings in my layout template

What I tried instead was creating
- a new custom control (a "ButtonContent") control, with two properties Text
and Icon. (subclass of ContentControl)
- a layout control template (target type ButtonContent) which displayed text
in a textblock using a TemplateBinding to the Text property of the target
type. (ditto for the icon)
- an overall control template (target type MyButton) which contained one of
these "ButtonContent" controls. I would specify values for the "Text" and
"Icon" properties of the ButtonContent using TemplateBinding to the
ButtonText and ButtonIcon properties of the actual MyButton.

Hope this makes sense so far. In effect I'm passing parameters from the
MyButton to the ButtonContent control.

This approach seems pretty rational to me BUT it does not work because
apparently custom controls cannot be used in control templates. So when I
attempt to put a local:ButtonContent control in my top level control
templates (just like I would any other control like a TextBlock, for example)
I get compiler errors as if I had no mapping set up for local:

It would be nice if I could dispense with the custom control and do all this
in markup. That is, if a control template could take arguments, so that it
could be deployed into another conttrol template with properties specified.
like
<Control Template={StaticResource Layout23} param1="sometext" />

but I'm not sure if that is possible and if it is, how exactly the other end
of it works (ie how to specify in a control template that it takes parameters
from outside).


The other possible way is to perhaps try and deploy a control of some sort
in a style. But it seems the same restriction applies - I cannot reference a
local:MyControl instance inside a style.



Hope this is a bit clearer.

Thanks

--
Griff
(trying to make an industrial UI with XAML/WPF/c#)


"lee" wrote:

> following is an example on how you can do this
> <Window x:Class="winyah.Window2"
> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
> xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
> Title="winyah" Height="600" Width="600"
> xmlns:local="clr-namespace:winyah" Name="window" Background="Aquamarine"
> >
> <Window.Resources>
>
> <local:ImageConverter1 x:Key="qa2"></local:ImageConverter1>
> <ControlTemplate x:Key="st1" TargetType="{x:Type local:MyButton}">
> <Border Width="120" Height="50" BorderBrush="black" BorderThickness="2">
> <Grid Height="50" ShowGridLines="true" Width="100">
> <Grid.ColumnDefinitions>
> <ColumnDefinition/>
> <ColumnDefinition/>
> </Grid.ColumnDefinitions>
> <Image Grid.Column="0" Source="{Binding RelativeSource={RelativeSource
> TemplatedParent}, Path=ButtonIcon, Converter={StaticResource qa2} }">
> </Image>
> <TextBlock Grid.Column="1" Text="{TemplateBinding ButtonText}"/>
> </Grid>
> </Border>
> </ControlTemplate>
> </Window.Resources>
> <StackPanel>
> <local:MyButton ButtonIcon="a1.gif" ButtonText="a button"
> Template="{StaticResource st1}">
>
> </local:MyButton>
> <local:MyButton ButtonIcon="a2.gif" ButtonText="another 1"
> Template="{StaticResource st1}">
>
> </local:MyButton>
> </StackPanel>
>
> </Window>
>
> public class MyButton:Button{
> public static DependencyProperty ButtonIconProperty =
> DependencyProperty.Register(
> "ButtonIcon",
> typeof(string), typeof(MyButton)
> );
> public string ButtonIcon
> {
> get { return (string)GetValue(ButtonIconProperty); }
> set { SetValue(ButtonIconProperty, value); }
> }
> public static DependencyProperty ButtonTextProperty =
> DependencyProperty.Register(
> "ButtonText",
> typeof(string), typeof(MyButton)
> );
> public string ButtonText
> {
> get { return (string)GetValue(ButtonTextProperty); }
> set { SetValue(ButtonTextProperty, value); }
> }
>
>
> }
> public class ImageConverter1 : IValueConverter
> {
> public object Convert(object value, Type targetType, object parameter,
> System.Globalization.CultureInfo culture)
> {
> BitmapImage myBitmapImage = new BitmapImage();
>
>
> myBitmapImage.BeginInit();
> myBitmapImage.UriSource = new Uri(value.ToString(), UriKind.
> RelativeOrAbsolute);
>
> myBitmapImage.DecodePixelWidth = 200;
> myBitmapImage.EndInit();
>
> return myBitmapImage;
>
> }
>
> public object ConvertBack(object value, Type targetType, object
> parameter, System.Globalization.CultureInfo culture)
> {
> return null;
> }
> }
>

My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
Old templates to new Vista General
templates Microsoft Office
Templates Vista 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