tags:

views:

30

answers:

2

Hi iam trying to progam an listboxitem Selected event in code because i need dynamical listboxitems.Iam coding this in wpf it works great.

<ListBoxItem Tag="cPage_Mod_Modules" Selected="ListBoxItem_Selected">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Style="{StaticResource sColor01}" Text="» " />
                                <TextBlock Text="Moduler" VerticalAlignment="Center" Focusable="True" />
                            </StackPanel>
                        </ListBoxItem>

the Selected="ListBoxItem_Selected" works fine.

But when iam trying to create the listboxitem in code. it doest work heres my code

            IList<ListBoxItem> lbi = new List<ListBoxItem>();

        ListBoxItem itemBox = new ListBoxItem();
        itemBox.Tag = "cPage_Assignment_Overview";
        itemBox.Selected += new EventHandler(ListBoxItem_Selected(this, null));

        lbTask.Items.Add(itemBox);

I just want to route to the event ListBoxItem_Selected(object sender, RoutedEventArgs e) when somebody is selecting an item. Sorry for my bad english hope you guys understand.

+1  A: 

Do you mean how to wire up the event ? This should do it (assuming the function signature is compatible with the event handler signature ).

itemBox.Selected += ListBoxItem_Selected;
Gishu
it is working now thanks
Tan
+1  A: 

Try changing

itemBox.Selected += new EventHandler(ListBoxItem_Selected(this, null));

to

itemBox.Selected += ListBoxItem_Selected;

I am assuming your ListBoxItem_Selected is declared like this

 public void ListBoxItem_Selected(object sender,RoutedEventArgs e)
 {

 }
Andrew Keith