2009-10-26 5 views
0

ListBoxItems이 동적이기 때문에 코드에 ListBoxItemSelected 이벤트를 프로그래밍하려고합니다. 내가 WPF이 코딩하고, 다음 XAML 잘 작동합니다 :C# program selected 코드의 이벤트

<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> 

Selected="ListBoxItem_Selected"가 잘 작동합니다.

그러나 코드에서 ListBoxItem을 만들려고해도 작동하지 않습니다. 누군가가 항목을 선택하면 난 그냥 이벤트 ListBoxItem_Selected(object sender, RoutedEventArgs e)로 라우팅 할

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); 

: 여기 내 코드입니다.

답변

1

이벤트를 연결하는 방법을 의미합니까? 이 작업을 수행해야합니다 (함수 시그니처가 이벤트 처리기 시그니처와 호환된다고 가정).

itemBox.Selected += ListBoxItem_Selected; 
+0

덕분에 같은 선언 – Tan

1

한번에 변경

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

itemBox.Selected += ListBoxItem_Selected; 

나는 당신의 ListBoxItem_Selected을 가정하고

에이 지금 노력하고 있습니다

public void ListBoxItem_Selected(object sender,RoutedEventArgs e) 
{ 

}