题
我正在尝试在代码中编写 ListBoxItem
Selected
事件,因为我需要动态 ListBoxItems
。我在wpf编码,以下xaml工作得很好:
<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 =&quot; ListBoxItem_Selected&quot;
正常工作。
但是当我尝试在代码中创建 ListBoxItem
时,它不起作用。这是我的代码:
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);
当有人选择项目时,我只想路由到 ListBoxItem_Selected(对象发送者,RoutedEventArgs e)
事件。
解决方案
你的意思是如何连接事件?这应该这样做(假设函数签名与事件处理程序签名兼容)。
itemBox.Selected += ListBoxItem_Selected;
其他提示
尝试更改
itemBox.Selected += new EventHandler(ListBoxItem_Selected(this, null));
到
itemBox.Selected += ListBoxItem_Selected;
我假设您的ListBoxItem_Selected声明为此
public void ListBoxItem_Selected(object sender,RoutedEventArgs e)
{
}
不隶属于 StackOverflow