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 - how to use a dynamicresource to set the scaletransform centerx and centrey

 
 
Old 07-25-2007   #1 (permalink)
Terraslate


 
 

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


 
 

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


 
 

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
 

Thread Tools



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