wpf 如何在列表框项模板中捕获按钮单击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24045581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:50:43  来源:igfitidea点击:

How to trap button click event inside listbox item template

wpflistbox

提问by NewInDevelopment

I have listbox with textbox inside to display list items. I want to have editable textbox , So I put button inside listbox for selected textbox. I want to trigger button click event for that listbox but I couldn't. Here is my code:

我有一个带有文本框的列表框来显示列表项。我想要可编辑的文本框,所以我将按钮放在列表框内以用于选定的文本框。我想为该列表框触发按钮单击事件,但我不能。这是我的代码:

         <ListBox HorizontalAlignment="Left" Name="ListTwo" Height="auto" Margin="134.53,15.934,0,0" VerticalAlignment="Top" Width="202.308" ItemsSource="{Binding . ,Source=CollectionUrl,BindsDirectlyToSource=True}" BorderThickness="0" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="100" />
                        <ColumnDefinition Width="50" />
                    </Grid.ColumnDefinitions>
                    <RadioButton>
                        <TextBox Name="TextBoxList" Text="{Binding Path=urlString, Mode=TwoWay}" Width="150">
                            <TextBox.Style>
                                <Style TargetType="TextBox">
                                    <Style.Triggers>
                                        <Trigger Property="IsFocused" Value="True">
                                            <Setter Property="Foreground" Value="Black"/>
                                            <Setter Property="IsReadOnly" Value="False" />
                                            <Setter Property="BorderThickness" Value="0.5"/>
                                        </Trigger>
                                        <Trigger Property="IsFocused" Value="False">
                                            <Setter Property="Foreground" Value="Gray"/>
                                            <Setter Property="IsReadOnly" Value="True" />
                                            <Setter Property="BorderThickness" Value="0"/>
                                        </Trigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBox.Style>
                        </TextBox>
                    </RadioButton>
                    <Button Content="Save" Grid.Column="1" Tag="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}}" Click="Button_Click_2">
                        <Button.Style>
                            <Style TargetType="Button">
                                <Setter Property="Visibility" Value="Collapsed" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding ElementName=TextBoxList, Path=IsFocused}" Value="True">
                                        <Setter Property="Visibility" Value="Visible" />
                                        <Setter Property="ClickMode" Value="Press"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Button.Style>
                    </Button>
                </Grid>
            </DataTemplate>

        </ListBox.ItemTemplate>

    </ListBox>

Anyone knows how can I trap click event of button?

任何人都知道如何捕获按钮的单击事件?

回答by 123 456 789 0

You have several options to do it but I'll just give you the two best ones without using third party frameworks.

您有多种选择可以做到这一点,但我只会在不使用第三方框架的情况下为您提供两个最好的选择。

First is through System.Windows.Interactionsand put it inside the TextBox. This will be handled in your ViewModel

首先是通过System.Windows.Interactions并将其放入TextBox. 这将在您的ViewModel

<i:Interaction.Triggers>
            <i:EventTrigger EventName="Click" >
                <i:InvokeCommandAction Command="{Binding ClickCommand}" />
            </i:EventTrigger>
</i:Interaction.Triggers>

Second is through using EventSetter, this will be handled behind the View

其次是通过 using EventSetter,这将在后面处理View

<Style TargetType="ListBoxItem">
             <EventSetter Event="MouseDoubleClick" Handler="TextBoxClick"/>
</Style>

回答by Rhyous

Did you try adding an event for Button in the ListBox. This will capture the event.

您是否尝试为 ListBox 中的 Button 添加事件。这将捕获事件。

Button.Click="OnClick"

So like this:

所以像这样:

<ListBox HorizontalAlignment="Left" Name="ListTwo" Height="auto" Margin="134.53,15.934,0,0" VerticalAlignment="Top" Width="202.308" ItemsSource="{Binding . ,Source=CollectionUrl,BindsDirectlyToSource=True}" BorderThickness="0" Button.Click="OnClick">

Then your event:

那么你的活动:

private void OnClick(object sender, RoutedEventArgs e)
{
    var originalSource = e.OriginalSource;
    // Do your work here
}

However, you have a second problem. Your button style is preventing your event from firing. It works without the style but doesn't fire with the style. Change the style to be on ListBoxItem.IsSelected. Then make it so if you select a TextBox the ListBoxItem is selected.

但是,您还有第二个问题。您的按钮样式正在阻止您的事件触发。它在没有风格的情况下工作,但不会随着风格而火。将样式更改为 ListBoxItem.IsSelected。然后,如果您选择一个 TextBox,则选择 ListBoxItem。

<ListBox HorizontalAlignment="Left" Name="ListTwo" Height="auto" Margin="134.53,15.934,0,0" VerticalAlignment="Top" Width="202.308" ItemsSource="{Binding . ,Source=CollectionUrl,BindsDirectlyToSource=True}" BorderThickness="0" Button.Click="OnClick">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100" />
                    <ColumnDefinition Width="50" />
                </Grid.ColumnDefinitions>
                <RadioButton>
                    <TextBox Name="TextBoxList" Text="{Binding Path=urlString, Mode=TwoWay}" Width="150">
                        <TextBox.Style>
                            <Style TargetType="TextBox">
                                <Style.Triggers>
                                    <Trigger Property="IsFocused" Value="True">
                                        <Setter Property="Foreground" Value="Black"/>
                                        <Setter Property="IsReadOnly" Value="False" />
                                        <Setter Property="BorderThickness" Value="0.5"/>
                                    </Trigger>
                                    <Trigger Property="IsFocused" Value="False">
                                        <Setter Property="Foreground" Value="Gray"/>
                                        <Setter Property="IsReadOnly" Value="True" />
                                        <Setter Property="BorderThickness" Value="0"/>
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </TextBox.Style>
                    </TextBox>
                </RadioButton>
                <Button Content="Save" Grid.Column="1">
                    <Button.Style>
                        <Style TargetType="Button">
                            <Setter Property="Visibility" Value="Collapsed" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
                                    <Setter Property="Visibility" Value="Visible" />
                                    <Setter Property="ClickMode" Value="Press"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Button.Style>
                </Button>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsKeyboardFocusWithin, RelativeSource={RelativeSource Self}}" Value="True">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(ListBoxItem.IsSelected)">
                                    <DiscreteBooleanKeyFrame KeyTime="0" Value="True"/>
                                </BooleanAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

Hope that helps.

希望有帮助。