数据绑定到表单的宽度

时间:2013-09-16 15:54:49

标签: c# wpf data-binding layouttransform

我正在尝试动态扩展基于WPF的应用程序的UI,我想使用当前分辨率与原始分辨率的比率,使用类似这样的内容:

    <Grid.LayoutTransform>
        <ScaleTransform CenterX="0" CenterY="0" ScaleX="{Binding FormWidth/NativeREsolution}" ScaleY="{Binding FormWidth/NativeREsolution}"/>
    </Grid.LayoutTransform>

我找到缩放变换的原因是它会缩放容器中的所有UI元素,包括框架和子页面。

无论如何都要这样做?

或者,是否有更好的方法根据窗口的大小动态缩放应用程序?

2 个答案:

答案 0 :(得分:2)

WPF is Resolution Independent by nature.

<Window ..>
   <Grid>

   <!-- Content here -->

   </Grid>
</Window>

上述XAML将导致Grid延伸到窗口大小。没有可怕的winforms般的黑客攻击。

修改

如果您希望Everything(包括字体大小)在Window内缩放,只需使用Viewbox:

<Window>
   <Viewbox>
       <Grid>

       <!-- Content here -->

       </Grid>
   </Viewbox>
</Window>

答案 1 :(得分:0)

最简单的解决方案是使用Viewbox,但也可以非常简单地计算Window调整大小的比例。

<强>视框

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Viewbox Stretch="Uniform">
    <Grid>
        <Label>
            Hello world
        </Label>
    </Grid>
</Viewbox>
</Window>

手动缩放

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" 
    Name="myMainWindow"
    Width="200" Height="250">
<Grid Name="MainGrid" SizeChanged="MainGrid_SizeChanged">
    <Grid.LayoutTransform>
        <ScaleTransform x:Name="ApplicationScaleTransform"
                        CenterX="0"
                        CenterY="0"
                        ScaleX="{Binding ElementName=myMainWindow, Path=ScaleValue}"
                        ScaleY="{Binding ElementName=myMainWindow, Path=ScaleValue}" />
    </Grid.LayoutTransform>
    <Grid VerticalAlignment="Center" HorizontalAlignment="Center" Height="150">
        <TextBlock FontSize="20" Text="Hello World" Margin="5" VerticalAlignment="Top" HorizontalAlignment="Center"/>
        <Button Content="Button" VerticalAlignment="Bottom" HorizontalAlignment="Center"/>
    </Grid>
</Grid>

使用LayoutTransform缩放网格,确定ScaleX&amp;通过引用在Window_Resize事件期间使用类似

的代码计算的编码的ScaleValue依赖项属性来实现ScaleY属性
#region ScaleValue Dependency Property

    public static readonly DependencyProperty ScaleValueProperty = DependencyProperty.Register("ScaleValue", typeof(double), typeof(MainWindow), new UIPropertyMetadata(1.0, new PropertyChangedCallback(OnScaleValueChanged), new CoerceValueCallback(OnCoerceScaleValue)));

    private static object OnCoerceScaleValue(DependencyObject o, object value)
    {
        MainWindow mainWindow = o as MainWindow;
        if (mainWindow != null)
            return mainWindow.OnCoerceScaleValue((double)value);
        else
            return value;
    }

    private static void OnScaleValueChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        MainWindow mainWindow = o as MainWindow;
        if (mainWindow != null)
            mainWindow.OnScaleValueChanged((double)e.OldValue, (double)e.NewValue);
    }

    protected virtual double OnCoerceScaleValue(double value)
    {
        if (double.IsNaN(value))
            return 1.0f;

        value = Math.Max(0.1, value);
        return value;
    }

    public double ScaleValue
    {            
        get
        {
            return (double)GetValue(ScaleValueProperty);
        }
        set
        {
            SetValue(ScaleValueProperty, value);
        }
    }
    #endregion


    private void CalculateScale()
        {
            double yScale = ActualHeight / 250f;
            double xScale = ActualWidth / 200f;
            double value = Math.Min(xScale, yScale);
            ScaleValue = (double)OnCoerceScaleValue(MainGrid, value);
        }