【发布时间】: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 已从问题中删除了该行 - 所以我删除了我的答案