使用XAML中的样式将MenuItem的Icon属性值设置为Shapes.Path

时间:2011-08-08 10:18:01

标签: wpf xaml styles expression-blend

我正在使用Silverlight 4和Expression Blend 4。

我正在设置一个MenuItem,其路径为Icon。

<Style x:Key="1234" TargetType="Controls:MenuItem">
        <Setter Property="Header" Value="Cancel" />
        <Setter Property="Icon">
            <Setter.Value>
                <Path Fill="#FF2B2B2B"
                      Width="14.3775"
                      Height="14"
                      Canvas.Left="0.311264"
                      Canvas.Top="0.500005"
                      Stretch="Fill"
                      Data="F1 M 111.2,-85.6L 124.2,-85.6L 124.2,-72.6L 111.2,-72.6L 111.2,-85.6 Z M 117.791,-82.3439L 117.791,-80.4506L 112.685,-80.4506L 112.685,-77.8084L 117.791,-77.8084L 117.791,-75.8748L 121.025,-79.1094L 117.791,-82.3439 Z M 121.657,-84.0222L 121.657,-74.0222L 122.657,-74.0222L 122.657,-84.0222L 121.657,-84.0222 Z " />
            </Setter.Value>
        </Setter>
    </Style>

但是当我尝试打开它时,这会给出一个异常表达式混合。

异常消息:错误HRESULT已从调用COM组件返回E_FAIL 在MS.Internal.XcpImports.MethodEx(IntPtr ptr,String name,CValue [] cdData)

当我将此样式添加到ContextMenu的项目时,这也会在运行时提供异常。

1 个答案:

答案 0 :(得分:4)

将样式放在控件模板中:

<ControlTemplate x:Key="CancelIcon">
      <Canvas>
        <Path Fill="#FF2B2B2B"
                          Width="14.3775"
                          Height="14"
                          Canvas.Left="0.311264"
                          Canvas.Top="0.500005"
                          Stretch="Fill"
                          Data="F1 M 111.2,-85.6L 124.2,-85.6L 124.2,-72.6L 111.2,-72.6L 111.2,-85.6 Z M 117.791,-82.3439L 117.791,-80.4506L 112.685,-80.4506L 112.685,-77.8084L 117.791,-77.8084L 117.791,-75.8748L 121.025,-79.1094L 117.791,-82.3439 Z M 121.657,-84.0222L 121.657,-74.0222L 122.657,-74.0222L 122.657,-84.0222L 121.657,-84.0222 Z " />
      </Canvas>
    </ControlTemplate>

然后为菜单尝试以下

<MenuItem Header="Cancel" >
  <MenuItem.Icon>
    <ContentControl Template="{StaticResource CancelIcon}" />
  </MenuItem.Icon>
</MenuItem>

希望这会成功: - )