16x16 pixel images in RibbonApplicationMenuItem gets stretched

陌路散爱 提交于 2019-12-12 01:08:37

问题


I am having a ribbon application menu that looks like this:

<ribbon:RibbonWindow>
  <DockPanel>
    <ribbon:Ribbon DockPanel.Dock="Top">
      <ribbon:Ribbon.ApplicationMenu>
        <ribbon:RibbonApplicationMenu>
          <ribbon:RibbonApplicationMenuItem Header="Users"                      
              ImageSource="Users16x16.png"
              Command="{Binding FooBinding}"/>
        </ribbon:RibbonApplicationMenu>
      </ribbon:Ribbon.ApplicationMenu>
    </ribbon:Ribbon>
  </DockPanel>
</ribbon:RibbonWindow>

The resulting image looks like this, stretched.

So how do I have a ribbon application menu item with a height that adapts to the image size instead of stretching?


回答1:


Change the control template like:

<ribbon:RibbonApplicationMenuItem Command="{Binding FooBinding}">
  <ribbon:RibbonApplicationMenuItem.Template>
    <ControlTemplate>
      <Grid >
        <Grid.ColumnDefinitions>
          <ColumnDefinition />
          <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Image Grid.Column="0" Source="Users16x16.png"/>
        <TextBlock Grid.Column="1">Users</TextBlock>
      </Grid>
    </ControlTemplate>
  </ribbon:RibbonApplicationMenuItem.Template>
</ribbon:RibbonApplicationMenuItem>

If you want to use control template as Style in your dictionary:

<Style x:Key="16x16ImageStyle" TargetType="{x:Type ribbon:RibbonApplicationMenuItem}">
 <Setter Property="Template">
  <Setter.Value>
   <ControlTemplate TargetType="{x:Type ribbon:RibbonApplicationMenuItem}">
    <Grid >
     <Grid.ColumnDefinitions>
      <ColumnDefinition />
      <ColumnDefinition />
     </Grid.ColumnDefinitions>
     <Image  VerticalAlignment="Center"  HorizontalAlignment="Center" Stretch="None"
             Grid.Column="0" Source="{TemplateBinding ImageSource}"/>
     <TextBlock Grid.Column="1" Text="{TemplateBinding Header}"></TextBlock>
    </Grid>
   </ControlTemplate>
  </Setter.Value>
 </Setter>
</Style>

and recall it in your ribbon:

<ribbon:RibbonApplicationMenuItem Header="Users"                      
          ImageSource="Users16x16.png"
          Command="{Binding FooBinding}" 
          Style="{StaticResource 16x16ImageStyle}"/>


来源:https://stackoverflow.com/questions/11898118/16x16-pixel-images-in-ribbonapplicationmenuitem-gets-stretched

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!