Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all,

I've got an animation that uses the ThicknessAnimation property to set the "To" and "From" aspects of the animation.

From="1000,0,-1000,0"

Now rather than hardcode the numbers I wish to bind a variable in XAML to the current or Actual screenwidth and then use this in place of the 1000 & -1000 values above.

How on earth can I do this in XAML?

I know how to declare an integer variable,

<system:int32 x:key="iScreenWidth" xmlns:x="#unknown" xmlns:system="#unknown">1024

but this doesn't represent the screenwidth as it resizes, nor do I know how to insert it into the From property to represent both 1000 & -1000.



Your help is really appreciated,



Jib
Posted

I will start by discouraging you from using the screen resolution, because:
1) the resolution can be changed at anytime(as you state)
2) the application can be moved to another screen
3) the application can in worst case be placed such that it is partly shown on 4 different screens.

That said first you should create a Property which you can bind to
C#
public Thickness MyFrom
        {
            get { return (Thickness) GetValue(MyFromProperty); }
            set { SetValue(FromProperty, value);}
        }

        public static DependencyProperty MyFromProperty =
            DependencyProperty.Register("MyFromProperty", typeof (Thickness), typeof (MyClass));


You can then use SystemEvents.DisplaySettingsChanged In order to detect display changes (including resolution changes) and update your MyFrom property

Futhermore you would have to track the movement of your window so you know which screen(s) it's currently being displayed on.
 
Share this answer
 
Comments
Jibrohni 12-Aug-11 9:03am    
I'm not looking to use resolution - I'm looking to use the current width of the MainWindow.xaml - is that not different? I thought I'd be able to bind an integer variable to the ActualWidth property of the Window and use that in the ThicknessAnimation definitions.
Jibrohni 12-Aug-11 9:28am    
Sorry, I did specify screen width, when I meant MainWindow width.
Espen Harlinn 6-Oct-11 10:38am    
5'ed!
Ok, someone gave some great direction on another forum, so credit here goes to Min Zhu, just wanted to share it as it's a popular question it seems:

I first created a value converter for Width to Thickness:

C#
[ValueConversion(typeof(double), typeof(Thickness))]
    public class ThicknessConverterFrom : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double width = (double)value;
            Thickness t = new Thickness(width, 0, 0 - width, 0);

            return t;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Thickness t = (Thickness)value;

            double width = new double();
            width = t.Left;

            return width;
        }
    }
    [ValueConversion(typeof(double), typeof(Thickness))]
    public class ThicknessConverterTo : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double width = (double)value;
            Thickness t = new Thickness(0 - width, 0, width, 0);

            return t;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Thickness t = (Thickness)value;

            double width = new double();
            width = t.Left;

            return width;
        }
    }


Then bound it as follows:

<ThicknessAnimation Duration="0:0:.5" Storyboard.TargetProperty="Margin" To="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, Path=ActualWidth, Converter={StaticResource thicknessConvFrom}}" AccelerationRatio=".9"/>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900