質問

動的な ListBoxItems が必要なため、コード内で ListBoxItem Selected イベントをプログラムしようとしています。私はこれを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(object sender、RoutedEventArgs e)にルーティングしたいだけです。

役に立ちましたか?

解決

イベントを接続する方法を意味しますか?これはそれを行う必要があります(関数シグネチャがイベントハンドラシグネチャと互換性があると仮定します)。

itemBox.Selected += ListBoxItem_Selected;

他のヒント

変更を試みてください

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

to

itemBox.Selected += ListBoxItem_Selected;

ListBoxItem_Selectedがこのように宣言されていると仮定しています

 public void ListBoxItem_Selected(object sender,RoutedEventArgs e)
 {

 }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top