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

how to use a dynamicresource to set the scaletransform centerx and centrey

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 07-25-2007   #1 (permalink)
Terraslate
Guest


 

how to use a dynamicresource to set the scaletransform centerx and centrey

is this even possible? from what i have read about dynamic resources they
seem just what i need - if i understand it properly.

finding an example of who used one to achieve anything like this is however
a nightmare. my preumption is that i could refer to a property on an object
i defined in xaml - so i want the scametransform centerx to be the width of
the button / 2 and the centery to be the height of the button /2.

i have no idea how to acheive this. anyone know how or have i completly
missed the mark with what DynamicResources are?

cheers



My System SpecsSystem Spec
Old 08-02-2007   #2 (permalink)
thelemmings@gmail.com
Guest


 

Re: how to use a dynamicresource to set the scaletransform centerx and centrey

Hi,

DynamicResources are not used for that kind of calculations. In order
to do what you want, you'll need a Binding, and a Converter:

First, make your CenterX and CenterY dependency properties (DP) bind
to their parent's Width and Height (in this case, a Button, 1 level
above):
<Button.RenderTransform>
<ScaleTransform
CenterX="{Binding Path=ActualWidth,Converter={StaticResource
DivideDoubleByTwoConverter},RelativeSource={RelativeSource
FindAncestor, GroupBox, 1}}"
CenterY="{Binding Path=ActualHeight,Converter={StaticResource
DivideDoubleByTwoConverter},RelativeSource={RelativeSource
FindAncestor, GroupBox, 1}}"/>
</Button.RenderTransform>

Note that the binding uses a Converter, which is a class implementing
IValueConverter. This class' sole use is to divide a double by two,
and return the result. Which in turn will be used in the binding:

[ValueConversion(typeof(double), typeof(double))]
public class DivideDoubleByTwoConverter: IValueConverter
{
public object Convert(object value, Type targetType, object
parameter, System.Globalization.CultureInfo culture)
{
if (targetType != typeof(double))
throw new InvalidOperationException("The target must
be a double");

double d = (double) value;
return ((double) d )/ 2;
}

public object ConvertBack(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}

Now, the last and only thing left to do is to declare
DivideDoubleByTwoConverter in your accessible resources so that you
can use it in your XAML file as a StaticResource. I personnally
declare those converters in the Window's or Page's resources, as I
tend to reuse them quite a lot:

<... xmlns:local="clr-namespace:MyNameSpace.MyAssembly" ...>

<Window.Resources>
<localivideDoubleByTwoConverter
x:Key="DivideDoubleByTwoConverter"/>
</Window.Resources>

It's quite complicated, and I hope the WPF team will add a possible
value for CenterX and CenterY (or maybe just Center as a Point?) so
that ScaleTransforms and RotateTransforms can be automatically
centered in the control!
Luc

On Jul 25, 9:37 pm, "Terraslate"
<terraslate.terrasl...@hotmail.hotmail.com.com> wrote:
> is this even possible? from what i have read about dynamic resources they
> seem just what i need - if i understand it properly.
>
> finding an example of who used one to achieve anything like this is however
> a nightmare. my preumption is that i could refer to a property on an object
> i defined in xaml - so i want the scametransform centerx to be the width of
> the button / 2 and the centery to be the height of the button /2.
>
> i have no idea how to acheive this. anyone know how or have i completly
> missed the mark with what DynamicResources are?
>
> cheers



My System SpecsSystem Spec
Old 09-18-2007   #3 (permalink)
Terraslate
Guest


 

Re: how to use a dynamicresource to set the scaletransform centerx and centrey

thanks very much - sorry for the delay in responding

i haven't tried it yet - but it looks good. since then i have read an
excellent book by chris sells on wpf - now it all makes sense. like you - i
hope they sort out the centrex, centreY issue as its a pretty common
scenario to want those things smack bang in the centre of the control.


<thelemmings@xxxxxx> wrote in message
news:1186093781.584481.37350@xxxxxx
Quote:

> Hi,
>
> DynamicResources are not used for that kind of calculations. In order
> to do what you want, you'll need a Binding, and a Converter:
>
> First, make your CenterX and CenterY dependency properties (DP) bind
> to their parent's Width and Height (in this case, a Button, 1 level
> above):
> <Button.RenderTransform>
> <ScaleTransform
> CenterX="{Binding Path=ActualWidth,Converter={StaticResource
> DivideDoubleByTwoConverter},RelativeSource={RelativeSource
> FindAncestor, GroupBox, 1}}"
> CenterY="{Binding Path=ActualHeight,Converter={StaticResource
> DivideDoubleByTwoConverter},RelativeSource={RelativeSource
> FindAncestor, GroupBox, 1}}"/>
> </Button.RenderTransform>
>
> Note that the binding uses a Converter, which is a class implementing
> IValueConverter. This class' sole use is to divide a double by two,
> and return the result. Which in turn will be used in the binding:
>
> [ValueConversion(typeof(double), typeof(double))]
> public class DivideDoubleByTwoConverter: IValueConverter
> {
> public object Convert(object value, Type targetType, object
> parameter, System.Globalization.CultureInfo culture)
> {
> if (targetType != typeof(double))
> throw new InvalidOperationException("The target must
> be a double");
>
> double d = (double) value;
> return ((double) d )/ 2;
> }
>
> public object ConvertBack(object value, Type targetType,
> object parameter, System.Globalization.CultureInfo culture)
> {
> throw new NotSupportedException();
> }
> }
>
> Now, the last and only thing left to do is to declare
> DivideDoubleByTwoConverter in your accessible resources so that you
> can use it in your XAML file as a StaticResource. I personnally
> declare those converters in the Window's or Page's resources, as I
> tend to reuse them quite a lot:
>
> <... xmlns:local="clr-namespace:MyNameSpace.MyAssembly" ...>
>
> <Window.Resources>
> <localivideDoubleByTwoConverter
> x:Key="DivideDoubleByTwoConverter"/>
> </Window.Resources>
>
> It's quite complicated, and I hope the WPF team will add a possible
> value for CenterX and CenterY (or maybe just Center as a Point?) so
> that ScaleTransforms and RotateTransforms can be automatically
> centered in the control!
> Luc
>
> On Jul 25, 9:37 pm, "Terraslate"
> <terraslate.terrasl...@xxxxxx> wrote:
Quote:

>> is this even possible? from what i have read about dynamic resources
>> they
>> seem just what i need - if i understand it properly.
>>
>> finding an example of who used one to achieve anything like this is
>> however
>> a nightmare. my preumption is that i could refer to a property on an
>> object
>> i defined in xaml - so i want the scametransform centerx to be the width
>> of
>> the button / 2 and the centery to be the height of the button /2.
>>
>> i have no idea how to acheive this. anyone know how or have i completly
>> missed the mark with what DynamicResources are?
>>
>> cheers
>
>

My System SpecsSystem Spec
Closed Thread
Update your Vista Drivers Update Your Drivers Now!!

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Applying a centered ScaleTransform via a Style Pascal Bourque Avalon 3 02-21-2006 01:37 PM
"The Freezable can not be frozen.." with {DynamicResource {x:Static SystemColors.ControlDarkDarkColorKey}} Keith Patrick Avalon 3 02-05-2006 12:34 PM



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