Hi i am new to wpf and i want to add controls in a grid using xaml at runtime based on some events like selecting a contextmenuitem is it possible if yes please provide some code snippets. I want to it in xaml only.
Asked
Active
Viewed 461 times
-1
-
no as of now i don't know its like i am having one grid containing 3 rows in 2nd row i will be having one button once i click on that button, i want to add one text box in grid.row=0 – user_111 Jun 25 '13 at 15:47
-
You should consider binding in XAML, over creating in C# (as we did in WinForms). More info: http://stackoverflow.com/questions/1002604/xaml-or-c-sharp-code-behind and http://stackoverflow.com/questions/7404943/programmatically-create-grid-with-custom-element – Ed Chapel Jun 25 '13 at 15:53
1 Answers
0
You cannot add controls at runtime using XAML only, but you can set control's Visibility using EventTriggers:
<StackPanel>
<Button Content="First">
<Button.Triggers>
<EventTrigger RoutedEvent="{x:Static Button.ClickEvent}">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="Second" Storyboard.TargetProperty="Visibility" Duration="0">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
<Button x:Name="Second" Content="Second" Visibility="Collapsed"/>
</StackPanel>
I think that this approach is awful, but it works :) It is much more simpler to add control from the code-behind.

Alexis
- 815
- 6
- 15