如何在WPF Datagrid单元格中输入的每个值上执行事件

时间:2015-11-03 12:15:07

标签: wpf wpfdatagrid

每次写任何一个单词时,我都会尝试执行一个事件。例如,当我写' 123'在单元格中,我想在输入的每个值上运行三次事件。

我用了#34; TargetUpdated"事件和写1,事件运行成功,但当我写2再次3事件不运行。请参阅下面的代码:

private void maingrid_TargetUpdated(object sender, DataTransferEventArgs e)
        {

            try
            {
                DataGrid Currcell = sender as DataGrid;
                int index = Currcell.CurrentColumn.DisplayIndex;
                vm.SetLineTotals(vm.Tax, vm.DiscountPer);
            }
            catch
            {
            }
       }          

实现此行为的原因是为输入的每个值获取datagrid linetotal的总和。请任何人帮忙和指导,谢谢。

更新: 请从以下链接获取我想解释的视频。 Sample Video

2 个答案:

答案 0 :(得分:0)

尝试使用KeyDown事件而不是TargetUpdated。每按一次键,它都会调用该事件。

更多关键行为:https://stackoverflow.com/a/5871419/5147720

答案 1 :(得分:0)

据我所知,您需要一些机制来处理数据网格单元上的用户键盘输入。在这里我可以建议你;使用两个附加属性。 First是一个布尔值来启用处理机制,第二个是一个Action将支持视图模型的动作来执行处理本身。您可以通过DataGridCell的样式附加此功能。这是一个描述的解决方案:  1. Xaml:

<Window x:Class="soHelpProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:soHelpProject="clr-namespace:SoHelpProject"
    Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
    <soHelpProject:MainViewModel/>
</Window.DataContext>
<Grid>
    <DataGrid ItemsSource="{Binding Collection}" AutoGenerateColumns="False">
        <DataGrid.Resources>
            <Style TargetType="DataGridCell">
                <Setter Property="soHelpProject:Attached.IsReactsOnKeyDown" Value="True"></Setter>
                <Setter Property="soHelpProject:Attached.OnKeyDownAction" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.OnKeyDownAction}"></Setter>
            </Style>
        </DataGrid.Resources>
        <DataGrid.Columns>
            <DataGridTextColumn Width="120" Binding="{Binding Name}"></DataGridTextColumn>
            <DataGridTextColumn Width="120" Binding="{Binding Surname}"></DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid></Window>

2。附属物码:

public class Attached
{
    public static readonly DependencyProperty OnKeyDownActionProperty = DependencyProperty.RegisterAttached(
        "OnKeyDownAction", typeof (Action<object>), typeof (Attached), new PropertyMetadata(default(Action<object>)));

    public static void SetOnKeyDownAction(DependencyObject element, Action<object> value)
    {
        element.SetValue(OnKeyDownActionProperty, value);
    }

    public static Action<object> GetOnKeyDownAction(DependencyObject element)
    {
        return (Action<object>) element.GetValue(OnKeyDownActionProperty);
    }

    public static readonly DependencyProperty IsReactsOnKeyDownProperty = DependencyProperty.RegisterAttached(
        "IsReactsOnKeyDown", typeof (bool), typeof (Attached), new PropertyMetadata(default(bool), IsReactsOnKeyDownPropertyChangedCallback));

    private static void IsReactsOnKeyDownPropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs args)
    {
        var val = (bool)args.NewValue;
        var cell = sender as DataGridCell;
        if(cell == null) 
            return;
        if (val == false)
        {
            cell.KeyDown -= CellOnKeyDown;
        }
        else
        {
            cell.KeyDown += CellOnKeyDown;
        }

    }

    private static void CellOnKeyDown(object sender, KeyEventArgs keyEventArgs)
    {
        var cell = sender as DataGridCell;
        if (cell == null)
            return;
        var action = cell.GetValue(OnKeyDownActionProperty) as Action<object>;
        if (action == null) return;
        action(keyEventArgs);
    }

    public static void SetIsReactsOnKeyDown(DependencyObject element, bool value)
    {
        element.SetValue(IsReactsOnKeyDownProperty, value);
    }

    public static bool GetIsReactsOnKeyDown(DependencyObject element)
    {
        return (bool) element.GetValue(IsReactsOnKeyDownProperty);
    }
}

3。 ViewModel和型号代码:

    public class MainViewModel:BaseObservableObject
{
    private Action<object> _onKeyDownAction;
    private ObservableCollection<Person> _collection;

    public MainViewModel()
    {
        Collection = new ObservableCollection<Person>
        {
            new Person
            {
                Name = "John",
                Surname = "A"
            },
            new Person
            {
                Name = "John",
                Surname = "B"
            },
            new Person
            {
                Name = "John",
                Surname = "C"
            },
        };
        OnKeyDownAction = new Action<object>(KeyWasPressed);
    }

    private void KeyWasPressed(object o)
    {
        var args = o as KeyEventArgs;
        if(args == null)
            return;
        Debug.WriteLine(args.Key.ToString());
    }

    public Action<object> OnKeyDownAction
    {
        get { return _onKeyDownAction; }
        set
        {
            _onKeyDownAction = value;
            OnPropertyChanged();
        }
    }

    public ObservableCollection<Person> Collection  
    {
        get { return _collection; }
        set
        {
            _collection = value;
            OnPropertyChanged();
        }
    }
}

public class Person:BaseObservableObject
{
    private string _name;
    private string _surname;

    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            OnPropertyChanged();
        }
    }

    public string Surname
    {
        get { return _surname; }
        set
        {
            _surname = value;
            OnPropertyChanged();
        }
    }
}
  1. 首先尝试冷静下来,干得好,应用程序看起来很好,所以是一个xaml。
  2. 正如我所见 ALL 您的更新逻辑基于PreviewKeyDown,“预览”时仍有旧值。当值已经更改时,尝试将您的逻辑基于PreviewKeyUp或KeyUp。如果有帮助,请告诉我。
  3. 我希望这会对你有所帮助。 的问候,

相关问题