WPF按钮:如何解决双击触发点击事件的问题

时间:2019-06-05 17:05:34

标签: c# wpf visual-studio

我有一个WPF布局,其中包含工具栏中的几个按钮。单击“新建项目”按钮时,将创建另一行文本框。但是,必须单击两次删除按钮才能删除最后创建的行。我如何使单击“删除”的单击事件而不是双击?

这是XMAL代码:

<Window x:Class="Odan_Estimator_Tool.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Odan_Estimator_Tool"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<ScrollViewer VerticalScrollBarVisibility="Auto">
    <Grid ScrollViewer.CanContentScroll="True" Name="GridMain">
        <DockPanel>

            <ToolBarTray DockPanel.Dock="Top">

                <ToolBar Height="40" Margin="0,0,-1,0">
                    <Button Content="New Item" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.512,0.521" Height="19" Click="NewItem" Margin="0,10,0,0"/>
                    <Button Content="Delete" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Height="19" Margin="0,10,0,0" Click="DeleteItem"/>
                    <Button Content="Calculate" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75"  RenderTransformOrigin="-0.203,0.604" Height="19" Margin="0,9,0,0" />


                </ToolBar>


            </ToolBarTray>


        </DockPanel>




        <TextBox HorizontalAlignment="Left" Height="23" Margin="10,76,0,0" TextWrapping="Wrap" Text="{Binding Id, Mode=TwoWay}" VerticalAlignment="Top" Width="145"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="606,76,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="176"/>
        <Label Content="Item #" HorizontalAlignment="Left" Margin="10,45,0,0" VerticalAlignment="Top" Height="26" Width="145"/>
        <Label Content="Description" HorizontalAlignment="Left" Margin="262,45,0,0" VerticalAlignment="Top" Height="26" Width="226"/>
        <Label Content="Unit" HorizontalAlignment="Left" Margin="160,45,0,0" VerticalAlignment="Top" RenderTransformOrigin="-1.725,-4.203" Height="26" Width="102"/>
        <Label Content="Type" HorizontalAlignment="Left" Margin="493,45,0,0" VerticalAlignment="Top" RenderTransformOrigin="2.705,2.641" Height="26" Width="113"/>
        <Label Content="Est. Qty" HorizontalAlignment="Left" Margin="611,45,0,0" VerticalAlignment="Top" RenderTransformOrigin="4.5,1.203" Height="26" Width="158"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="259,76,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Width="229" />
        <TextBox HorizontalAlignment="Left" Height="23" Margin="155,76,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Width="104" />
        <TextBox HorizontalAlignment="Left" Height="23" Margin="488,76,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Width="118" RenderTransformOrigin="1.403,0.47"/>
        <ScrollViewer HorizontalAlignment="Left" Height="245" Margin="914,154,-506,0" VerticalAlignment="Top" Width="384"/>
        <Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="45" VerticalAlignment="Top" Width="792"/>



    </Grid>
</ScrollViewer>

这是C#代码,底部具有Delete功能。

    private void NewItem(object sender, RoutedEventArgs e)
    {


        //Generates new row of text boxes

        //Generates new item ID textbox
        TextBox item_box = new TextBox();
        item_box.Height = 23;
        item_box.Width = 161;
        item_box.Text = "";

        item_box.Margin = new Thickness(21, 76 + 23*counter, 0, 0);
        item_box.VerticalAlignment = 0;
        item_box.HorizontalAlignment = 0;

        items.Add(item_box);

        GridMain.Children.Add(item_box);

        //Generates new item ID textbox
        TextBox id_box = new TextBox();
        id_box.Height = 23;
        id_box.Width = 161;
        id_box.Text = "";

        id_box.Margin = new Thickness(21, 76 + 23 * counter, 0, 0);
        id_box.VerticalAlignment = 0;
        id_box.HorizontalAlignment = 0;

        items.Add(id_box);

        GridMain.Children.Add(id_box);



        counter++;
    }

    private void DeleteItem(object sender, RoutedEventArgs e)
    {

        GridMain.Children.Remove(items.ElementAt(items.Count - 1));
        items.RemoveAt(items.Count - 1);


    }

0 个答案:

没有答案