将XAML中的整数变量绑定到屏幕宽度并使用它 [英] Binding an integer variable in XAML to Screen Width and using it

查看:81
本文介绍了将XAML中的整数变量绑定到屏幕宽度并使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个动画,该动画使用ThicknessAnimation属性设置动画的"To"和"From"方面.

从="1000,0,-1000,0"

现在,我不想将数字硬编码,而是希望将XAML中的变量绑定到当前或实际屏幕宽度,然后使用它代替1000& -1000以上的值.

我到底如何在XAML中做到这一点?

我知道如何声明整数变量,

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

但这并不代表它在调整大小时的屏幕宽度,也不知道如何将其插入From属性中以代表1000& -1000.



非常感谢您的帮助,



Jib

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

推荐答案

首先,我将劝阻您不要使用屏幕分辨率,原因是:
1)分辨率可以随时更改(如您所述)
2)可以将应用程序移至另一个屏幕
3)在最坏的情况下,可以将应用程序放置在4个不同的屏幕上的一部分.

也就是说,首先您应该创建一个可以绑定到的属性
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
public Thickness MyFrom
        {
            get { return (Thickness) GetValue(MyFromProperty); }
            set { SetValue(FromProperty, value);}
        }

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



然后,您可以使用SystemEvents.DisplaySettingsChanged来检测显示更改(包括分辨率更改)并更新您的MyFrom属性

此外,您还必须跟踪窗口的移动,以便知道窗口当前正在显示在哪个屏幕上.



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.


好吧,有人在另一个论坛上给出了很好的指导,所以值得赞扬的是朱敏,他只是想分享它,因为这似乎是一个受欢迎的问题:

我首先为宽度到厚度创建了一个值转换器:

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:

[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"/>


这篇关于将XAML中的整数变量绑定到屏幕宽度并使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆