views:

1816

answers:

4

hello community any one can please say how to add controls dynamically into a Stack Panel

note: what i need is i have to create a menu which get data from database and creates menu items accordingly can any one say how can i create such menus i am new to silver light

I am using silverlight 3 beta and expression blend3 + sketch flow please help me to know how to design those

A: 

Excuse the variable names, but here is a code snippet of dynamically adding items to a stack panel

StackPanel split = new StackPanel();
TextBlock expected = new TextBlock();
expected.Text = "Expected Final Bonus";
TextBlock meh = new TextBlock();
meh.Text = Math.Round(((QuoteData)results.First()).ExpectedBonus * 100, 2) + "%";
split.Children.Add(expected);
split.Children.Add(meh);

TextBlock disc = new TextBlock();
disc.Text = "Discretionary Percentage";
TextBlock number = new TextBlock();
number.Text = Math.Round(((QuoteData)results.First()).Discretionary * 100, 2) + "%";

split.Children.Add(disc);
split.Children.Add(number);

Here you can see that I also created the stack panel dynamically, however, you can also create it using XAML.

Something like this should work:

<StackPanel Grid.Row="3" Grid.Column="1" Name="split" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto">
Johannes
A: 

First, if your StackPanel is already in your XAML, handle the Loaded event:

<StackPanel x:Name="spValue" Loaded="spValue_Loaded">
</StackPanel>

private void spValue_Loaded(object sender, RoutedEventArgs e)
{
    StackPanel stackPanel = (sender as StackPanel);
    stackPanel.Children.Clear();
    stackPanel.Children.Add(XamlReader.Load(XElement.Parse(xaml).ToString()) as FrameworkElement);
}

The controls are created with XAMLReader from your stuff loaded from the DB. You can adapt all that to your particular scenario (menu and menu items...)

devMomentum
friend devMomentum,i will load N number of items from DB how can i add can i use foreach or for loop in urs answer XElement is the variable which gets datafrom database am i right can i use some thing like this private void spValue_Loaded(object sender, RoutedEventArgs e){for(int i =0;i<count;i++){ StackPanel stackPanel = (sender as StackPanel); stackPanel.Children.Clear(); stackPanel.Children.Add(XamlReader.Load(XElement[i].Parse(xaml).ToString()) as FrameworkElement);}}i mean XElement is the array of string taken from DB can i use like this
NavinKumar.K.S
A: 
NavinKumar.K.S
A: 

check out this

http://silverlightpopupmenu.codeplex.com/

Tiju John