![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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> <local ivideDoubleByTwoConverterx: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 Specs![]() |
| | #3 (permalink) |
| | 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> > <local ivideDoubleByTwoConverter> 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 Specs![]() |