编辑模式下的WPF datagrid事件句柄

时间:2015-01-30 15:38:30

标签: c# wpf mvvm wpf-controls wpfdatagrid

我找到了一个非常体面的auto-complete textbox control,它可以完美地满足我的需要。但是只有一个问题试图在一个月内解决这个问题。

它在正常形式下可以很好地用作自动完成控制,但不能在网格内工作。

问题是当弹出自动完成时,我们需要双击该项以选择。但是在网格内我们双击进入编辑模式,当控件弹出时编辑模板,第一次单击网格关闭编辑模式,所以控件无法处理双击,任何方式绕过事件来控制而不是关闭网格编辑模式

我附上了一个非常简洁且准备好运行的示例来说明这个问题,如果有专家研究它会很有帮助。

由于

https://drive.google.com/file/d/0B6OtkpYGX9qfaEYzMEE3RjVQbEU/view?usp=sharing

1 个答案:

答案 0 :(得分:1)

不知道为什么TextBlock的行为与"名称选择器"相同。控制,但为什么你不能用"名称选择器替换Datagrid的TextBlock"?

我尝试了它并按预期工作:

<UserControl
             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:i="clr-namespace:SampleApp.Infrastructure"
             xmlns:View="clr-namespace:SampleApp.View"
             xmlns:ViewModel="clr-namespace:SampleApp.ViewModel"          
             xmlns:SampleApp="clr-namespace:SampleApp" 
    x:Class="SampleApp.View.FormControl"          
             mc:Ignorable="d"
             d:DataContext="{d:DesignInstance {x:Type ViewModel:MainViewModel}}" d:DesignWidth="541.198" d:DesignHeight="402.384">

    <UserControl.Resources>
        <Style x:Key="LabelTextBlockStyle"
               TargetType="{x:Type TextBlock}">
            <Setter Property="FontWeight"
                Value="Bold" />
            <Setter Property="HorizontalAlignment"
                Value="Right" />
            <Setter Property="Margin"
                Value="0,0,10,0" />
            <Setter Property="VerticalAlignment"
                Value="Center" />
        </Style>
    </UserControl.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <View:NamePicker Grid.Column="1" Margin="0,5,162,0" WatermarkText="Type 'a' key to search"
                 Grid.Row="1" x:Name="autoName" AutoSelectSingleResult="True" 
                 SelectedText="{Binding FirstName, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}">

        </View:NamePicker>

        <!--<View:NamePicker Grid.Column="1" Margin="0,5,10,0" 
                 Grid.Row="1" x:Name="autoNamea"
                 SelectedItem="{Binding FirstName, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"/>-->

        <TextBlock Grid.Column="0"
            Grid.Row="1"
            Text="First Name"
            Style="{StaticResource LabelTextBlockStyle}"
            Margin="0,5,10,0"/>
        <DataGrid CanUserAddRows="True" BorderBrush="Black" BorderThickness="1" ItemsSource="{Binding PersonList,Mode=TwoWay}"
            HorizontalAlignment="Left" AutoGenerateColumns="False"
                  VerticalAlignment="Top" Grid.Column="1" Margin="10,24,0,-161" Grid.Row="6" Height="141" Width="371">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Name">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <View:NamePicker SelectedText="{Binding Name,Mode=TwoWay}" VerticalAlignment="Center" HorizontalAlignment="Stretch" WatermarkText="Type 'a' key to Search"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <View:NamePicker VerticalAlignment="Stretch" HorizontalAlignment="Stretch" WatermarkText="Type 'a' key to search"
                                                                          SelectedText="{Binding Name, Mode=TwoWay}"
                                                                          SelectedValue="{Binding Name, Mode=TwoWay}">
                            </View:NamePicker>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
        <TextBlock HorizontalAlignment="Left" Margin="0,34,0,-151" Grid.Row="6" TextWrapping="Wrap" VerticalAlignment="Top" Height="181" Width="61"><Run Text="Data Grid"/><LineBreak/><Run Text="Double click on the first row to edit the item (intellibox control in the Grid in Name Column)"/><LineBreak/><Run/></TextBlock>
        <TextBlock Grid.Column="1" HorizontalAlignment="Left" Margin="10,211,0,-219" Grid.Row="6" TextWrapping="Wrap" Text="Once you type the 'a' you can choose down arrow key to select the item. it works. but when we use mouse to select the item its not binding the selected item in the GRID, in the first click the event goes to the grid and closing the popup. Any way to prevent this??" VerticalAlignment="Top" Width="371" Height="73"/>
        <TextBlock Grid.Column="1" Margin="339,9,14,0" Grid.Row="1" TextWrapping="Wrap" Text="Intellibox Control" VerticalAlignment="Top"/>
        <TextBlock Grid.Column="1" HorizontalAlignment="Left" Margin="10,7,0,0" Grid.Row="6" TextWrapping="Wrap" Text="The Custom control works well in the noraml form, but not working in the datagrid" VerticalAlignment="Top"/>
        <TextBlock Grid.Column="1" HorizontalAlignment="Left" Margin="10,308,0,-240" Grid.Row="6" TextWrapping="Wrap" Text="SelectedItem:" VerticalAlignment="Top"/>
        <TextBlock Grid.Column="1" HorizontalAlignment="Left" Margin="95,308,0,-220" Grid.Row="6" TextWrapping="Wrap" Text="{Binding FirstName,Mode=TwoWay}" FontWeight="Bold" VerticalAlignment="Top"/>



    </Grid>
</UserControl>