无法左键单击ListBox中的TextBox(使用拖放)WPF

时间:2017-12-26 12:33:46

标签: wpf textbox drag-and-drop listbox

我有一个包含TextBox的ListBox(还有图片和其他东西)。 问题是由于我为ListBox处理的previewmouseleftbuttondown,我无法在TextBox中单击。 这意味着该程序使用此函数处理我的左键单击,这就是为什么我不能在textBox中写入。 我该怎么办?

listBox:

ListBox x:Name="lbTwo" Grid.Column="2 " Margin="{StaticResource SplitRight}"
                 Drop="ListBox_Drop" AllowDrop="True"
                 PreviewMouseLeftButtonDown="ListBox_PreviewMouseLeftButtonDown" MouseDown="someListBox_MouseDown"
                 Style="{DynamicResource ResourceKey=styleLightListBox}"
                 ScrollViewer.VerticalScrollBarVisibility="Visible">

它包含的项目:

<DataTemplate x:Key="templateLightListBoxItem">
        <Grid Margin="5">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Border Grid.Column="0"
                    Grid.Row="0"
                    Grid.RowSpan="2"
                    Margin="0,0,10,0">
                <Image Source="{Binding Path=Number_pic}"
                       Stretch="Fill"
                       Height="40"
                       Width="40"></Image>
            </Border>
            <TextBlock Text="{Binding Path=Name}"
                       FontWeight="Bold"
                       Grid.Column="1"
                       Grid.Row="0"></TextBlock>
            <Grid 
                Grid.Column="1"
                Grid.Row="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"></ColumnDefinition>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Label x:Name="label" Content="Your number: " Grid.Column="0" />
                <TextBox x:Name="NumberTextBox" PreviewTextInput="NumberValidationTextBox" Text="{Binding Path=mainNumber, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" Width="20"/>
            </Grid>
        </Grid>
    </DataTemplate>

编辑:

private void ListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ListBox destLisBox = (ListBox)sender;
            dragSource = destLisBox;

            GiveFeedbackEventHandler handler = new GiveFeedbackEventHandler(DragSource_GiveFeedback);

            this.dragSource.GiveFeedback += handler;


            object data = GetDataFromListBox(dragSource, e.GetPosition(destLisBox));

            if (data != null)
            {
                DragDrop.DoDragDrop(destLisBox, data, DragDropEffects.Move);
            }
        }

private void DragSource_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
    try
    {
        //This loads the cursor from a stream ..
        if (_allOpsCursor == null)
        {
            {
                _allOpsCursor = new Cursor(path+@"\Icons\Red_Gear.cur");
            }
        }
        Mouse.SetCursor(_allOpsCursor);
        e.UseDefaultCursors = false;
        e.Handled = true;
    }
    finally { }
}

0 个答案:

没有答案