ToggleButton中的图像在WPF中无法正确显示

时间:2015-01-06 20:41:59

标签: c# wpf togglebutton

我试图在ToggleButton中的文字前放置图像。一切进行得都很顺利。但图像显示不正确。我在这里放大了图像,以便您可以看到我在说什么。如您所见,图像的某些部分无法正确显示。

我试图展示的图片

enter image description here

结果

enter image description here

缩放结果

enter image description here

<BitmapImage x:Key="ColumnsLayoutMiniIcon" UriSource="pack://application:,,,/Main/Resources/dark/images/mini/columns_layout_mini_icon.png"/>

                                            

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToggleButton">
                <StackPanel Orientation="Horizontal">
                    <Image x:Name="Border" Width="13" Height="13" Source="{StaticResource ColumnsLayoutMiniIcon}">
                    </Image>
                    <Border x:Name="Content"
                            Padding="10 0 10 0">
                        <ContentPresenter HorizontalAlignment="Center" 
                                          VerticalAlignment="Center" 
                                          Margin="-2 -3 0 0"
                                          RecognizesAccessKey="True" />
                    </Border>                        
                </StackPanel>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="Border" Property="Cursor" Value="Hand"/>
                    </Trigger>
                    <Trigger Property="IsChecked" Value="False">
                    </Trigger>
                    <Trigger Property="IsChecked" Value="True">

                    </Trigger>

                    <Trigger Property="IsChecked" Value="False">
                        <Setter Property="Foreground" Value="LightGray"/>
                    </Trigger>
                </ControlTemplate.Triggers>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<RadioButton Content="Plan View"
             GroupName="View"
             Style="{StaticResource BaseToggleButton}"
             Checked="SwitchToPlanView_Selected"
             IsEnabled="{Binding SwitchToModelViewsEnabled}"
             IsChecked="{Binding SwitchToPlanViewSelected}">
</RadioButton>

1 个答案:

答案 0 :(得分:3)

UseLayoutRounding="true"添加到Window元素的根目录中。这将确保图片捕捉到像素,并且不会显示奇怪的伪像。