question

Knox avatar image
0 Votes"
Knox asked ·

Issue displaying Canvas in Viewbox across multiple UserControls

UPDATE: Apparently I have to set x:Shared to False on my Canvas elements in App.xaml.

I have some Canvas elements set as StaticResources in App.xaml that draw various icons. I also have a custom UserControl that simply displays a Button whose content is a Viewbox with a ContentPresenter where I then set a StaticResource.

For example...

     <Button>
         <Viewbox Margin="4">
             <ContentPresenter Content="{StaticResource ResourceKey=IconAdd}" />
         </Viewbox>
     </Button>

This works as expected if I have just 1 instance of the UserControl, but if I put multiple instances only the last one actually draws the Canvas icon. Any ideas why?

For example...

 <StackPanel>
    
 <controls:MyUserControlButton /> <!-- No icon -->
 <controls:MyUserControlButton /> <!-- No icon -->
 <controls:MyUserControlButton /> <!-- Shows icon -->
    
 </StackPanel>


Thanks!

windows-wpf
10 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AlexLi-MSFT avatar image
0 Votes"
AlexLi-MSFT answered ·

Welcome to our Microsoft Q&A platform!

I think you better use Grid instead of Stackpanel.

  <Grid>
         <Grid.RowDefinitions>
             <RowDefinition/>
             <RowDefinition/>
             <RowDefinition/>
         </Grid.RowDefinitions>
         <controls:MyUserControlButton Grid.Row="0"/>
         <controls:MyUserControlButton Grid.Row="1"/>
         <controls:MyUserControlButton Grid.Row="2"/>
     </Grid>

Thanks.


10 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AdrianYankov-0681 avatar image
0 Votes"
AdrianYankov-0681 answered ·

I have the same issue. It looks like a bug. The icon is only shown in the last TreeViewItem for me. When I expand, it always moves it to the lower level.

10 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.