c# - WPF 数据绑定(bind)网格缩放

标签 c# wpf xaml mvvm grid

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

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

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

代码:
    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 缩放网格,
因为当我使窗口变小时,网格会停留在这些尺寸上,这会损害应用程序的缩放。

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

最佳答案

将它放在 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>

关于c# - WPF 数据绑定(bind)网格缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39976459/

相关文章:

c# - 我如何在 C# 中表示像 1/3 这样的分数?

c# - 将大型 MFC 应用程序迁移到 WPF/.NET 有哪些技术?

wpf - 如何从 GridView 中绑定(bind)到 ViewModel 的属性

c# - 确认字典中的所有键都已填充值

c# - 如何在 Eclipse 中导入 .net 库?

c# - WPF 将调试器传递给新生成的进程

xaml - 组合框选择内的网格对齐

c# - MahApps Metro HamburgerMenu 绑定(bind)不工作

C# 正则表达式 : How to extract a collection

wpf - 更新到 ListBox 绑定(bind)集合后刷新 UI