我有一个不行为的WPF数据网格。单击以编辑单元格时,不显示DataGridTemplateColumn.CellEditingTemplate。如果我在单元格内单击并按F2进行编辑,则会显示DataGridTemplateColumn.CellEditingTemplate。我试图使用this SO post中的代码将其设为单击编辑单元格。
private void PendingDetailsDataGrid_GotFocus( object sender, RoutedEventArgs e )
{
if ( e.OriginalSource.GetType() == typeof( DataGridCell ) )
{
DataGrid grd = ( DataGrid ) sender;
grd.BeginEdit( e );
}
}
我注意到if语句总是失败,因为DataGridCell的类型是Microsoft.Windows.Controls使用程序集(WPFToolkit.dll)而e.OriginalSource的类型是System.Windows.Controls(Presentation.dll)。如果我将鼠标悬停在XAML视图中的DataGrid声明上,它会显示System.Windows.Controls.DataGrid,那么DataGridCell的错误类型如何?我在另一个窗口中有一个几乎相同的设置,我复制了DataGrid和该表单中的引用但没有任何改变。
修改
<Controls:MetroWindow
x:Class="DFTI.Views.TransactionsWindow2"
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:Controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:vm="clr-namespace:DFTI.ViewModels"
xmlns:local="clr-namespace:DFTI.Views"
xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"
Dialog:DialogParticipation.Register="{Binding}"
Title="Transactions" Height="560" Width="900"
Name="Transactions"
WindowStartupLocation="CenterScreen">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Closing">
<cmd:EventToCommand Command="{Binding Path=ClosingCommand}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<DataGrid Grid.Row="0"
x:Name="PendingTransactions"
DataGridCell.Selected="PendingDetailsDataGrid_GotFocus"
ItemsSource="{Binding PendingTransactions}"
SelectedItem="{Binding PendingDetails}"
AutoGenerateColumns="False"
CanUserResizeColumns="True"
CanUserDeleteRows="False"
CanUserReorderColumns="True"
Margin="10,0,10,17" >
<DataGrid.Columns>
<DataGridTextColumn Header="Posting Date" Width="Auto" Binding="{Binding PostingDate}" IsReadOnly="True" />
<DataGridTextColumn Header="Status" Width="Auto" Binding="{Binding Status}" IsReadOnly="True" />
<DataGridTextColumn Header="Batch No" Width="Auto" Binding="{Binding BatchNo}" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
<DataGrid Grid.Row="1"
Height="200"
ItemsSource="{Binding PendingDetails.Details}"
AutoGenerateColumns="False"
CanUserAddRows="True"
CanUserDeleteRows="True"
CanUserResizeColumns="True"
CanUserReorderColumns="True"
DataGridCell.Selected="PendingDetailsDataGrid_GotFocus"
Margin="10,0">
<DataGrid.Columns>
<DataGridTemplateColumn x:Name="AccountNum" Header="Account No" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBox BorderThickness="0" Text="{Binding Path=Account.RawAccountNumber}"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<StackPanel>
<toolkit:AutoCompleteBox
Background="AliceBlue"
IsTextCompletionEnabled="True"
FilterMode="Contains"
MinimumPrefixLength="2"
ValueMemberPath="RawAccountNumber"
PreviewTextInput="AutoCompleteBox_PreviewTextInput"
SelectedItem="{Binding Account, Mode=TwoWay}"
Text="{Binding Path=Account.RawAccountNumber}"
ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Accounts}" >
<toolkit:AutoCompleteBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding Path=RawAccountNumber}" FontWeight="Bold" Width="100"/>
<TextBlock Text="{Binding Path=Description}" />
</StackPanel>
</DataTemplate>
</toolkit:AutoCompleteBox.ItemTemplate>
</toolkit:AutoCompleteBox>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Description" Width="Auto" Binding="{Binding Description}" IsReadOnly="True" />
<DataGridTemplateColumn x:Name="DebitAmt" Header="Debit">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding DAmount, StringFormat='$#,##0.00;$(#,##0.00)'}"
Width="100"
BorderThickness="0">
</TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<Controls:NumericUpDown Value="{Binding DAmount, Mode=TwoWay}" StringFormat="C2" Minimum="0" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn x:Name="CreditAmt" Header="Credit">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding CAmount, StringFormat='$#,##0.00;$(#,##0.00)'}"
Width="100"
BorderThickness="0">
</TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<Controls:NumericUpDown Value="{Binding CAmount, Mode=TwoWay}" StringFormat="C2" Minimum="0" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Controls:MetroWindow >
答案 0 :(得分:0)
Trt在if
语句中使用完全限定的类型名称。您不需要在此处涉及WPFToolkit.dll
。这对我有用:
private void PendingDetailsDataGrid_GotFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType() == typeof(System.Windows.Controls.DataGridCell))
{
DataGrid grd = (DataGrid)sender;
grd.BeginEdit(e);
}
}