Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将Interaction.Triggers设置为ListBoxItem_C#_Wpf_Xaml_Listbox_Listboxitem - Fatal编程技术网

C# 将Interaction.Triggers设置为ListBoxItem

C# 将Interaction.Triggers设置为ListBoxItem,c#,wpf,xaml,listbox,listboxitem,C#,Wpf,Xaml,Listbox,Listboxitem,我已将Interaction.Triggers设置为ListBox,并在“SelectionChanged”事件发生时执行相应的TargetedTrigger操作,如下所示 <ListBox x:Name="WorksheetListBox" ItemsSource="{Binding WorkSheetCollection}" ItemTemplate="{StaticResource workSheetTemplate}">

我已将Interaction.Triggers设置为ListBox,并在“SelectionChanged”事件发生时执行相应的TargetedTrigger操作,如下所示

<ListBox x:Name="WorksheetListBox" ItemsSource="{Binding WorkSheetCollection}"
                             ItemTemplate="{StaticResource workSheetTemplate}">                              
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="SelectionChanged">
        <action:WorksheetListBoxAction />
    </i:EventTrigger>
  </i:Interaction.Triggers>
</ListBox>

但我的要求是需要设置ListBoxItem的“PreviewMouseDown”事件的Interaction.Triggers(注意:通过ItemsSource填充ListBox)


您可以在不使用Interactivity.dll进行事件处理的情况下执行相同的操作。



您可以在不使用Interactive.dll进行事件处理的情况下执行相同的操作。

您可以在ListBoxItem上的PreviewMouseDown事件中执行此操作

<ListBox ItemsSource="{StaticResource Data}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Button Name="TaskButton" Content="{Binding}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <EventSetter Event="PreviewMouseDown"
                                 Handler="ItemOnPreviewMouseDown" />
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>

        private void ItemOnPreviewMouseDown(
            object sender, MouseButtonEventArgs e)
        {

            ((ListBoxItem) sender).IsSelected = true;

        }

私有无效项OnPreviewMouseDown(
对象发送器,鼠标按钮ventargs(e)
{
((ListBoxItem)sender).IsSelected=true;
}

您可以通过ListBoxItem上的PreviewMouseDown事件执行此操作

<ListBox ItemsSource="{StaticResource Data}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Button Name="TaskButton" Content="{Binding}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <EventSetter Event="PreviewMouseDown"
                                 Handler="ItemOnPreviewMouseDown" />
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>

        private void ItemOnPreviewMouseDown(
            object sender, MouseButtonEventArgs e)
        {

            ((ListBoxItem) sender).IsSelected = true;

        }

私有无效项OnPreviewMouseDown(
对象发送器,鼠标按钮ventargs(e)
{
((ListBoxItem)sender).IsSelected=true;
}

您可以尝试以下方法:

    <Style TargetType="{x:Type ListBoxItem}">
        <Style.Triggers>
            <EventTrigger RoutedEvent="PreviewMouseDown">
                <EventTrigger.Actions>
                    <action:WorksheetListBoxAction />
                </EventTrigger.Actions>
            </EventTrigger>
        </Style.Triggers>
    </Style>

您可以尝试以下方法:

    <Style TargetType="{x:Type ListBoxItem}">
        <Style.Triggers>
            <EventTrigger RoutedEvent="PreviewMouseDown">
                <EventTrigger.Actions>
                    <action:WorksheetListBoxAction />
                </EventTrigger.Actions>
            </EventTrigger>
        </Style.Triggers>
    </Style>


…您好,如果我将“SelectionChanged”替换为“PreviewMouseDown”,它将仅对ListBox触发,而不是对ListBoxItem触发。我需要为ListBoxItem设置此项。因此,请建议我必须在何处为ListBoxItem设置此“事件触发器”
…您好,如果我将“SelectionChanged”替换为“PreviewMouseDown”,它将仅对ListBoxItem触发,而不是对ListBoxItem触发。我需要为ListBoxItem设置此项。因此,请建议我必须在何处为ListBoxItem设置此“事件触发器”,这是如何工作的,甚至标记为已接受的答案?对于初学者来说,
action:WorksheetListBoxAction
是一个
System.Windows.Interactivity.TriggerAction
,它不能用作
EventTrigger
内部
Style.Triggers
的操作。也不可能重写action类以从
System.Windows.TriggerAction
派生,该类在这种情况下可以工作,但不幸的是没有公共构造函数可从中派生。这怎么可能工作,甚至被标记为可接受的答案?对于初学者来说,
action:WorksheetListBoxAction
是一个
System.Windows.Interactivity.TriggerAction
,它不能用作
EventTrigger
内部
Style.Triggers
的操作。也不可能重写action类以从
System.Windows.TriggerAction
派生,该类在这种情况下可以工作,但遗憾的是,它没有从中派生的公共构造函数。