【问题标题】:Set width/height to be proportional in xaml?在xaml中将宽度/高度设置为成比例?
【发布时间】:2013-09-28 08:27:58
【问题描述】:

我正在创建一个大小为 300x100 的用户控件,我希望它是 1024x768 屏幕上的分辨率,但在 2048x1536 屏幕上我希望它是 600x200,一般来说我喜欢将其大小设置为与高度差成比例(因此,如果屏幕是两倍高,则控件将是两倍高和两倍宽)。

有什么想法可以在不借助代码的情况下完成此任务(如果没有其他选择,我愿意)?

【问题讨论】:

标签: c# wpf xaml


【解决方案1】:

您可以通过创建代理控件来解决此问题,而无需隐藏代码。人们经常使用这种方法来正确更新他们可以绑定到的 ActualWidth 和 ActualHeight 属性,就像this question 的答案一样。

在您的情况下,您可能希望将代理控件的元素绑定到某个拉伸到完整窗口大小的元素。当绑定元素的宽度或高度发生变化时,您可以将代理控件上的两个属性的值设置为计算出的比例高度和宽度。然后,您可以为用户控件的宽度和高度绑定这些成比例的大小。

例如:

public class ProportionalSizeHelper : FrameworkElement, INotifyPropertyChanged
{
    private double _proportionalWidth = 0;
    private double _proportionalHeight = 0;

    public event PropertyChangedEventHandler PropertyChanged;

    public FrameworkElement Element
    {
        get { return (FrameworkElement)GetValue(ElementProperty); }
        set { SetValue(ElementProperty, value); }
    }

    public double ProportionalHeight
    {
        get{ return _proportionalHeight; }
    }

    public double ProportionalWidth
    {
        get { return _proportionalWidth; }
    }

    public static readonly DependencyProperty ElementProperty =
        DependencyProperty.Register("Element", typeof(FrameworkElement), typeof(ProportionalSizeHelper), 
                                    new PropertyMetadata(null,OnElementPropertyChanged));

    private static void OnElementPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((ProportionalSizeHelper)d).OnElementChanged(e);
    }

    private void OnElementChanged(DependencyPropertyChangedEventArgs e)
    {
        FrameworkElement oldElement = (FrameworkElement)e.OldValue;
        FrameworkElement newElement = (FrameworkElement)e.NewValue;

        if (newElement != null)
        {   
            newElement.SizeChanged += new SizeChangedEventHandler(Element_SizeChanged);
        }
        else
        {
            _proportionalHeight = 0;
            _proportionalWidth = 0;
        }
        if (oldElement != null)
        {
            oldElement.SizeChanged -= new SizeChangedEventHandler(Element_SizeChanged);
        }
        NotifyPropChange();
    }

    private void Element_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        // This can be whatever calculation you need
        double factor = Math.Min(Element.ActualWidth/1024, Element.ActualHeight/768);
        _proportionalHeight = 100*factor;
        _proportionalWidth = 300*factor;
        NotifyPropChange();
    }

    private void NotifyPropChange()
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs("ProportionalWidth".NonNls()));
            PropertyChanged(this, new PropertyChangedEventArgs("ProportionalHeight".NonNls()));
        }
    }
}

然后在 xaml 中,将 UserControl 更改为您需要的任何内容:

<Grid x:Name="LayoutRoot">
    <Grid.Resources>
        <c:ProportionalSizeHelper Element="{Binding ElementName=LayoutRoot}" x:Name="proxy" />
    </Grid.Resources>
    <UserControl Width="{Binding ProportionalWidth, ElementName=proxy}" Height={Binding ProportionalHeight, ElementName=proxy}  />
</Grid>

【讨论】:

    【解决方案2】:

    这是一个银光代码。下面是获取屏幕高度和宽度的方法

    public MainPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);
        }
        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            int Width = HtmlPage.Window.Eval("screen.width");
            int Height = HtmlPage.Window.Eval("screen.height");
    
    //Now here increase the height and width of control
    double factor = Width/1024; // this is your default you asked for
    gdTestElement.Width = 300*factor;
    gdTestElement.Height = 100*factor;
    
            }
    

    :) 试着告诉我

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-09
      • 2013-03-12
      • 2020-02-05
      • 2013-08-20
      • 1970-01-01
      • 2013-03-13
      • 1970-01-01
      • 2010-10-17
      相关资源
      最近更新 更多