仅将布局转换应用于父项,而不应用于任何特定的子项

本文关键字:应用于 任何特 转换 布局 | 更新日期: 2023-09-27 18:36:20

MainWindow.XAML:

<Window x:Class="TestWPFApp.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Name="myMainWindow"
            Title="MainWindow" 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" Name="myButton"/>
            </Grid>
        </Grid>
    </Window>

主窗口代码隐藏:

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        #region ScaleValue Depdency 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;
        }
        protected virtual void OnScaleValueChanged(double oldValue, double newValue)
        {
        }
        public double ScaleValue
        {
            get
            {
                return (double)GetValue(ScaleValueProperty);
            }
            set
            {
                SetValue(ScaleValueProperty, value);
            }
        }
        #endregion
        private void MainGrid_SizeChanged(object sender, EventArgs e)
        {
            CalculateScale();
        }
        private void CalculateScale()
        {
            double yScale = ActualHeight / 250f;
            double xScale = ActualWidth / 200f;
            double value = Math.Min(xScale, yScale);
            ScaleValue = (double)OnCoerceScaleValue(MainGrid, value);
        }
    }

我有此示例应用程序。我正在主网格上应用 LayoutTransform 以通过调整窗口大小来扩展应用程序。如何避免我的控件 myButton 应用该转换?我不希望这种转换应用于它。

仅将布局转换应用于父项,而不应用于任何特定的子项

两个选项

1)不要让它成为孩子,而只是让它在视觉上重叠,例如使用第二个网格。

2)将变换的逆向应用于子项,这将否定变换,可能很昂贵,但应该可以解决问题。

<Button LayoutTransform="{Binding ElementName=MainGrid, Path=LayoutTransform.Inverse}"/>

就个人而言,我会使用第一个选项。