【问题标题】:Viewbox behavior that I cannot understand我无法理解的 Viewbox 行为
【发布时间】:2009-06-04 02:14:24
【问题描述】:

我有一个 DataTemplate 用作描述版本对象的徽标。徽标只是一张图片和一些文字。

我必须在 2 个地方使用 DataTemplate: - 作为按钮的内容 - 作为 ListBox 的缩略图项

问题是,ListBox 中的徽标必须比 Button 中的小得多。这不会对自动缩放的徽标图像造成麻烦。但字体大小不会自动缩放。

因此建议我使用 ViewBox 作为整个徽标的 XAML 描述的父级。

这是数据模板:

        <DataTemplate DataType="{x:Type l:Version}">
            <Viewbox>
                <Grid Width="175">
                    <Image Source="{StaticResource Crate}"/>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition />
                        </Grid.RowDefinitions>

                        <TextBlock Grid.Row="0" FontSize="18" Text="{Binding Type}" />
                        <TextBlock Grid.Row="1" FontSize="20" Text="{Binding Platform}" />
                        <TextBlock Grid.Row="2" FontSize="20" Text="{Binding ApprovedVersion}" />
                        <TextBlock Grid.Row="3" FontSize="16" Text="{Binding StringFormat=C\{0\}, Path=CodeChangeList}" />
                        <TextBlock Grid.Row="4" FontSize="16" Text="{Binding StringFormat=D\{0\}, Path=DataChangeList}" />
                        <TextBlock Grid.Row="5" FontSize="16" Text="{Binding StringFormat=S\{0\}, Path=SoundChangeList}" />
                    </Grid>
                </Grid>
            </Viewbox>
        </DataTemplate>

然后我只需将按钮的 DataContext 更改为 Version 对象即可使用此模板。然后会自动使用 DataTemplate。

             <Button 
                x:Name="buttonVersion"
                Width="300"
                Height="300"
                Click="SelectVersionButton_Click"
                Background="Transparent"
                Content="{Binding}">
             </Button>

为什么我的徽标没有填满整个按钮?显然有空白来自哪里,我无法解释这一点。

有趣的是,如果我从 Grid 子项(ViewBox 的子项)中删除 Width="175",那么 ViewBox 将完全填充按钮。 但是,我的徽标的字体大小不再正确缩放,因为这个 Grid Width 就像 ViewBox 应该用来适当缩放其余部分的“参考大小”......

提前致谢

【问题讨论】:

  • 你对我的第一反应是正确的。我想在我到处指责它的行为之前我应该​​使用一个控件。已删除回复以避免混淆任何人。
  • 我询问了网格边距 - 但它不相关,我现在看到 Thibaut 已从问题中删除了该行 - 所以我删除了我的答案

标签: wpf viewbox


【解决方案1】:

好的,问题是这样解决的:

  1. 删除 DataTemplate 网格中的 Width=175
  2. 再次调整字体大小

我的字体大小已针对 175x175 的板条箱图像进行了调整。板条箱图像为 256x256。如果您未在 Grid 元素中指定 Width=175,则由于图像的原始大小 (256x256),ViewBox 似乎会计算比率。此外,由于 ViewBox,按钮 300x300 现在完全被板条箱图像填充,字体大小也相应增加。

所以在第 2 步中,我必须为 256x256 图像而不是 175x175 图像调整字体大小。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-05
    • 2019-11-08
    • 1970-01-01
    • 1970-01-01
    • 2012-11-07
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多