WPF数据绑定网格缩放

时间:2016-10-11 11:41:50

标签: c# wpf xaml mvvm grid

我想将数据绑定到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

1 个答案:

答案 0 :(得分:0)

将其放入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>