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

时间:2014-06-04 19:06:03

标签: wpf listbox

我的文件框里面有列表框来显示列表项。我想要有可编辑的文本框,所以我将按钮放在列表框中以选择文本框。我想触发该列表框的按钮点击事件,但我不能。这是我的代码:

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

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

2 个答案:

答案 0 :(得分:2)

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

Button.Click="OnClick"

所以这样:

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

然后你的活动:

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

然而,你有第二个问题。您的按钮样式会阻止您的事件被触发。它没有风格,但没有风格。将样式更改为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>

希望有所帮助。

答案 1 :(得分:1)

你有几种方法可以做到这一点,但我会在没有使用第三方框架的情况下给你最好的两个。

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

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

其次是使用EventSetter,这将在View

后面处理
<Style TargetType="ListBoxItem">
             <EventSetter Event="MouseDoubleClick" Handler="TextBoxClick"/>
</Style>