【问题标题】:How to create an icon from static resource twice?如何从静态资源创建图标两次?
【发布时间】:2015-11-17 19:15:30
【问题描述】:

我在Application.Resources(svg 图标)中有一个资源:

<Canvas x:Key="IconAdd" Width="48" Height="48">
...

我正在尝试在多个按钮中使用它:

<Button>
    <StackPanel>
        <Viewbox>
            <ContentPresenter Content="{StaticResource IconAdd}"/>
        </Viewbox>
        <TextBlock>Button text</TextBlock>
    </StackPanel>
</Button>

问题是图标只出现在最后一个按钮中。

answer 我了解到这是因为在 WPF 中每个 Visual 只能有一个父级。

我觉得我需要在数据模板中添加一个图标,但在我的情况下如何使用它?

【问题讨论】:

  • 发生这种情况是因为 Canvas 是静态的(所有应用程序的一个实例),您可以尝试使用 DataTemplate,我不确定如何,但应该是这样

标签: c# wpf xaml


【解决方案1】:

您可以尝试使用x:Shared 属性。这是SO QuestionMSDN site的链接

【讨论】:

    【解决方案2】:

    正如我所料,我需要在数据模板中添加一个图标:

     <DataTemplate x:Key="IconAddTemplate">
         <Canvas Width="48" Height="48">
    ...
    

    然后我可以这样使用它:

    <Button>
        <StackPanel>
            <Viewbox>
                <ContentPresenter ContentTemplate="{StaticResource IconAddTemplate}" />
            </Viewbox>
            <TextBlock>Button text</TextBlock>
        </StackPanel>
    </Button>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-28
      • 1970-01-01
      • 2012-08-07
      • 1970-01-01
      • 2011-06-16
      • 1970-01-01
      • 2021-02-27
      • 2021-01-21
      相关资源
      最近更新 更多