【问题标题】:WPF Databound Grid ScalingWPF 数据绑定网格缩放
【发布时间】:2016-10-11 11:41:50
【问题描述】:

我想将数据绑定到xaml 中的网格。这是我用来绑定它的代码:

    <Grid  x:Name="Example" Grid.Column="1" Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  >
        <ContentPresenter Content="{Binding ExampleImage}" />
    </Grid>

现在,当我使用固定宽度和高度绑定它时,它会以设定的尺寸显示网格。

代码:

    private Grid _exampleImage;

    public Grid ExampleImage
    {
        get
        {
            if (SelectedSectie != null)
            {
                var convertFromString = System.Windows.Media.ColorConverter.ConvertFromString("#CCCCCC");
                if (convertFromString != null)
                {
                    DropShadowEffect dse = new DropShadowEffect
                    {
                        BlurRadius = 5,
                        ShadowDepth = 1,
                        Direction = 270,
                        Color =
                            (System.Windows.Media.Color)
                            convertFromString
                    };

                    _exampleImage = new Grid
                    {
                        Background =
                            new SolidColorBrush(SingleIcons.Helpers.ColorConverter.ToMediaColor(SelectedSectie.Color.ColorValue)),
                        VerticalAlignment = VerticalAlignment.Stretch,
                        Width = SelectedIconSize.Width,
                        Height = SelectedIconSize.Height,
                        MaxWidth = SelectedIconSize.Width,
                        MaxHeight = SelectedIconSize.Height,
                        HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch,
                        Effect = dse,
                        RowDefinitions =
                        {
                            new RowDefinition {Height = new GridLength(46, GridUnitType.Star)},
                            new RowDefinition {Height = new GridLength(3, GridUnitType.Star)}
                        }
                    };
                }

                TextBlock afbeeldingTextBlock = new TextBlock
                {
                    Text = _selectedSectie.Sectie,
                    TextWrapping = TextWrapping.Wrap,
                    TextAlignment = TextAlignment.Center,
                    Width = _exampleImage.Width,
                    FontSize = Global.Fontsize,
                    FontFamily = new System.Windows.Media.FontFamily(Global.Family.Name),
                    VerticalAlignment = VerticalAlignment.Center,
                    Foreground =
                        new SolidColorBrush(
                            SingleIcons.Helpers.ColorConverter.ToMediaColor(SelectedSectie.Color.TextColorValue)),

                };

                TextOptions.SetTextFormattingMode(afbeeldingTextBlock, TextFormattingMode.Display);
                TextOptions.SetTextRenderingMode(afbeeldingTextBlock, TextRenderingMode.ClearType);

                Canvas bottomCanvas = new Canvas
                {
                    Background = (SolidColorBrush) (new BrushConverter().ConvertFrom("#26000000"))
                };

                Grid.SetRow(afbeeldingTextBlock, 0);
                Grid.SetRow(bottomCanvas, 1);

                _exampleImage.Children.Add(afbeeldingTextBlock);
                _exampleImage.Children.Add(bottomCanvas);

                _exampleImage.Measure(new System.Windows.Size(_exampleImage.Width,
                    _exampleImage.Height));
                _exampleImage.Arrange(
                    new Rect(new System.Windows.Size(_exampleImage.Width, _exampleImage.Height)));


                return _exampleImage;
            }
            else
            {
                return null;
            }

        }
    }

现在我想做的是将网格保持在设定的尺寸,但允许 GUI 缩放网格, 因为当我缩小窗口时,网格会停留在这些尺寸上,这会损害应用程序的缩放。

我使用数据绑定网格的原因是我有另一个函数可以将此网格导出到.png

【问题讨论】:

    标签: c# wpf xaml mvvm grid


    【解决方案1】:

    将它放在 Viewbox 中并将 StretchDirection 设置为 DownOnly 是我想要的结果。

    <Grid  x:Name="Example" Grid.Column="1" Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  >
            <Viewbox StretchDirection="DownOnly" >
                <ContentPresenter Content="{Binding ExampleImage}" />
            </Viewbox>
    </Grid>
    

    【讨论】:

      猜你喜欢
      • 2011-09-28
      • 2010-11-15
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      • 2012-07-03
      • 2013-11-14
      相关资源
      最近更新 更多