我在WPF程序中有一个TreeView,每个TreeViewItem上都有一个图像。当我将此图像尺寸设置为21x21或更大时,它显示得很好-我可以看到整个图像。但是,如果我将其设置为20x20或更小,它会破裂-它只占用最右边的像素并将其拉伸到我输入的宽度,所以我看不到整个图像。
我创建了一个新的项目进行测试,并且注意到在放大/缩小设计器窗口时,图像从正常翻转为破裂。
这似乎是一些与DPI或缩放有关的怪异错误。
这是我添加到TreeView中的原始图像: https://ibb.co/p3tHFJM
这是显示问题的屏幕截图。上方图片为20x20,下方图片为21x21。 https://ibb.co/7vJjVS5
这里有一些演示代码,您可以在计算机上尝试。您可能必须将尺寸设置得较小,例如5x5才能看到问题。还尝试在设计器中放大/缩小。
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfBug"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TreeView Grid.Row="0" Grid.Column="0" Background="Pink" >
<TreeView.Resources>
<BitmapImage x:Key="PlayIcon" UriSource="/Resources/play.png" />
</TreeView.Resources>
<TreeViewItem Header="These subitems work correctly">
<Button>
<Image Source="{StaticResource PlayIcon}" Width="25" Height="25" />
</Button>
<TextBlock Text="This image is size 25" />
</TreeViewItem>
<TreeViewItem Header="These subitems have broken images">
<Button >
<Image Source="{StaticResource PlayIcon}" Width="11" Height="11" />
</Button>
<TextBlock Text="This image is size 11" />
</TreeViewItem>
</TreeView>
</Grid>
</Window>