ControlTemplate中的重复边框

时间:2013-02-05 08:25:09

标签: c# wpf controltemplate

我遇到控制模板的问题。

<ControlTemplate x:Key="{ComponentResourceKey ResourceId=SubmenuItemTemplateKey,TypeInTargetAssembly={x:Type MenuItem}}" TargetType="{x:Type MenuItem}">
    <Grid Margin="19 12.5">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Border BorderThickness="0.25,0.25,2,2" BorderBrush="Blue" Background="{StaticResource ControlBrush}" CornerRadius="5" HorizontalAlignment="Center">
            <ContentPresenter ContentSource="Icon" Width="64" Height="64" Margin="5"/>
        </Border>
        <ContentControl Grid.Row="1" FontSize="10" HorizontalContentAlignment="Center" Foreground="#2A57A5" Name="Header" HorizontalAlignment="Center">
            <ContentPresenter ContentSource="Header"/>
        </ContentControl>
    </Grid>
</ControlTemplate>

图片:http://www.woodworm.ch/TemplateProblem1.png

此模板中的边框重复,一旦包含图像,一旦它为空。 不需要的边框在下图中标有红色圆圈:

http://www.woodworm.ch/TemplateProblemMarked.png

当我取消注释边框时,我明白了:

        <!--<Border BorderThickness="0.25,0.25,2,2" BorderBrush="Blue" Background="{StaticResource ControlBrush}" CornerRadius="5" HorizontalAlignment="Center">-->
            <ContentPresenter ContentSource="Icon" Width="64" Height="64" Margin="5"/>
        <!--</Border>-->

图片:http://www.woodworm.ch/TemplateProblem2.png

最后,如果我取消注释图像的ContentPresenter,我会得到以下结果:

        <Border BorderThickness="0.25,0.25,2,2" BorderBrush="Blue" Background="{StaticResource ControlBrush}" CornerRadius="5" HorizontalAlignment="Center">
            <!--<ContentPresenter ContentSource="Icon" Width="64" Height="64" Margin="5"/>-->
        </Border>

图片:same url, but TemplateProblem3.png

有人可以重现这种奇怪的行为,或者有人知道为什么这个边界是重复的。 它是整个应用程序中唯一一个带蓝色边框画笔的边框,用于检查边框是否来自其他地方。

<LinearGradientBrush StartPoint="0,0" EndPoint="0,1" x:Key="ControlBrush">
    <GradientStop Color="{x:Static SystemColors.ControlColor}" Offset="0"/>
    <GradientStop Color="{x:Static SystemColors.ControlLightColor}" Offset="1"/>
</LinearGradientBrush>

    <MenuItem Header="{Binding Title}"
              Command="{Binding NavigateToCommand,Source={StaticResource MainViewModel}}"
              CommandParameter="{Binding}"
              IsEnabled="{Binding IsEnabled}">
        <MenuItem.Icon>
            <Image Source="{Binding Icon}"/>
        </MenuItem.Icon>
    </MenuItem>

创建MenuItem的数据模板:

<DataTemplate DataType="{x:Type oag:HMIPage}" x:Key="PageMenuItemTemplate">
    <MenuItem Header="{Binding Title}"
              Command="{Binding NavigateToCommand,Source={StaticResource MainViewModel}}"
              CommandParameter="{Binding}"
              IsEnabled="{Binding IsEnabled}">
        <MenuItem.Icon>
            <Image Source="{Binding Icon}"/>
        </MenuItem.Icon>
    </MenuItem>
</DataTemplate>

0 个答案:

没有答案