Home > Back-end >  WPF how to set stackpanel as resources and reuse it in TabControls
WPF how to set stackpanel as resources and reuse it in TabControls

Time:11-10

I am new to C# and WPF so please give me some ideas:

I have an WPF app used to display some stack panels,all stack panels default Visibility is set to collapsed and they will switch to visible according to the received data.

Now I want to make all these stack panels to resources so I can reuse it in some new added tab controls and stack panels.

<StackPanel x:Name="ColorOption" Visibility="Collapsed">
  <TextBlock Text="Line Color" Style="{StaticResource ItemNameTextBlockStyle}"/>
  <Button Style="{StaticResource ColorButtonStyle}" Click="Color_Click">
    <Button.Content>
      <Rectangle x:Name="LineColorRect" Style="{StaticResource ColorSelectionRectangleStyle}" />
    </Button.Content>
  </Button>
</StackPanel>

Above is one example of stack panels I am using. In the code behind the function "Color_Click" will change this "ColorOption" stack panel state and do something.

However after I try to put this stack panel into Windows.Resources

<Window.Resources>
  <StackPanel x:Name="ColorOption" Visibility="Collapsed" x:Key="ColorOption">
    <TextBlock Text="Line Color" Style="{StaticResource ItemNameTextBlockStyle}"/>
    <Button Style="{StaticResource ColorButtonStyle}" Click="Color_Click">
      <Button.Content>
        <Rectangle x:Name="LineColorRect" Style="{StaticResource ColorSelectionRectangleStyle}" />
     </Button.Content>
    </Button>
  </StackPanel>
</Window.Resources> (I also put the style files inside)

In the tab controls I did

<TabControl>
  <TabItem Header="Tab 1" Content="{StaticResource ColorOption}"/>
</TabControl>

The visual studio shows error in the code behind says "ColorOption does not exist in the current context"

How can I fix this? Is any way to set the context? thank you

CodePudding user response:

You can simply wrap the StackPanel in ContentControl and make it ControlTemplate.

<ControlTemplate x:Key="ColorOptionTemplate" TargetType="{x:Type ContentControl}">
    <StackPanel x:Name="ColorOption" Visibility="Collapsed">
        <TextBlock Text="Line Color" Style="{StaticResource ItemNameTextBlockStyle}"/>
        <Button Style="{StaticResource ColorButtonStyle}" Click="Color_Click">
            <Button.Content>
                <Rectangle x:Name="LineColorRect" Style="{StaticResource ColorSelectionRectangleStyle}"/>
            </Button.Content>
        </Button>
    </StackPanel>
</ControlTemplate>

However, you will need to change properties of controls inside the ContentControl and it would be cumbersome. So the StackPanel could be wrapped in UserControl instead.

<UserControl x:Class="WpfApp1.ColorOptionControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel x:Name="ColorOption" Visibility="Collapsed">
        <TextBlock Text="Line Color" Style="{StaticResource ItemNameTextBlockStyle}"/>
        <Button Style="{StaticResource ColorButtonStyle}" Click="Color_Click">
            <Button.Content>
                <Rectangle x:Name="LineColorRect" Style="{StaticResource ColorSelectionRectangleStyle}"/>
            </Button.Content>
        </Button>
    </StackPanel>
</UserControl>

This is a common way in WPF. The downside is that you will need to add dependency properties to UserControl and wire up them with dependency properties of internal controls so that you can set their values at the level of UserControl and bridge them with external controls and window. This could be complicated and cumbersome as well.

So I think ideally it would be better to find an existing control which has similar functionalities you want and create a custom control deriving from the existing one.

  •  Tags:  
  • wpf
  • Related