Amit Kohan

Amit Kohan

  • NA
  • 4
  • 0

Adding item to expander in WPF

Jun 11 2012 5:57 PM

Hello experts,

In part of my code I need to add some object(s) (let's say rectangle) to an <expander> which is embedded into a <stackpanel>. So far, I have done it using <style>, <setter> and can show a rectangle on the expander. However, I must be able to insert those rectangles dynamically.

Currently the way it works now is that I've created a ResourceDictionary in a file with XAML file extension
 
  <s:myToolbox x:Key="FC" ItemSize="90,80" SnapsToDevicePixels="True"
  ScrollViewer.HorizontalScrollBarVisibility="Disabled">


and then using (referenced) it in Window1.xaml as following.

  <StackPanel>
  <Label Name="lblFind">Search:</Label>
  <StackPanel Name="spFind" Orientation="Horizontal">
  <TextBox Name="txtFind" Width="200" Height="22" ></TextBox>
 
  <Button Content="Search" Width="60" Height="30" IsEnabled="{Binding ElementName=txtFind, Path=Text.Length, Mode=OneWay}" Click="Button_Click" />
  </StackPanel>
  <Expander x:Name="expander1" Header="Controls" Content="{StaticResource FC}" IsExpanded="True"/>
  <Expander Header="A"  IsExpanded="True"/>
  <Expander Header="B" IsExpanded="True"/>
  <Expander Header="C"  IsExpanded="True"/>
  </StackPanel>


This way I can setup some styles and assign them to rectangle and then display them on toolbox (expander control). Now, my question is how I could create the same thing but dynamically using C# code?

Thanks,
Amit