代码之家  ›  专栏  ›  技术社区  ›  Antoine V

在研究文本框上按Enter后,第一个结果数据报的wpf自动单击按钮

  •  -1
  • Antoine V  · 技术社区  · 8 年前

    我正在做一个WPF MVVM项目。数据报显示服务找到的记录。每一行都有一个按钮,用于执行选定记录的操作。

    public class ReferenceDossier
    {
    }
    

    XAML

    <GroupBox>
        <Grid>
            <StackPanel>
                <TextBox>
                    <TextBox.InputBindings>
                        <KeyBinding Key="Enter" Command="{Binding SearchCommand}"/>
                    </TextBox.InputBindings>
                </TextBox>
            </StackPanel>
        </Grid>
    </GroupBox>
    
    <GroupBox>
        <Grid>
            <StackPanel>
                <DataGrid ItemsSource="{Binding ReferenceDossiers}">
                    <DataGrid.Columns>
                        <DataGridTemplateColumn>
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Button Command="{Binding Command}"
                                                    CommandParameter="{Binding}">>
                                        <iconPacks:PackIconFontAwesome Kind="PlusCircle" Foreground="#FF94bf00"/>
                                    </Button>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                    </DataGrid.Columns>
                </DataGrid>
            </StackPanel>
        </Grid>
    </GroupBox>
    

    当我使用 TextBox 点击Enter,它执行计算并将结果显示在 DataGrid

    Command Button

    2 回复  |  直到 8 年前
        1
  •  0
  •   dhilmathy    8 年前

    你可以用 EventTrigger 为了达到这个目的。

    XAML

    <Style TargetType="DataGridRow">
        <Style.Triggers>
            <local:EnterDownEventTrigger EventName="KeyDown">
                <i:InvokeCommandAction Command="{Binding Command}" CommandParameter="{Binding}"/>
            </local:EnterDownEventTrigger>
        </Style.Triggers>
    </Style>
    

    如你所愿 Command 仅在上调用 进入 键,您需要编写自定义 事件触发器 为了这个。

    Enter键的自定义事件触发器

    using System.Windows.Interactivity;
    public class EnterDownEventTrigger : EventTrigger 
    {    
        public EnterDownEventTrigger() : base("KeyDown") { }
    
        protected override void OnEvent(EventArgs eventArgs) 
        {
            var e = eventArgs as KeyEventArgs;
            if (e != null && e.Key == Key.Enter)
                this.InvokeActions(eventArgs);
        }
    }
    

    引用程序集: system.windows.interactivity.dll(系统.windows.interactivity.dll)

        2
  •  0
  •   Antoine V    8 年前

    找到的解决方案:

    在这之后 answer ,我们可以通过自定义属性将焦点设置为控件 IsFocused .

    所以我把这个属性放在按钮上,如果是第一行,就为这个属性设置为true,然后绑定Enter InputBinding

    public class ReferenceDossier : INotifyPropertyChanged
        {
            private bool _selected;
            public bool Selected
            {
                get
                {
                    return _selected;
                }
                set
                {
                    _selected = value;
                    RaisePropertyChanged("Selected");
                }
            }
    }
    

    集合 Selected 使第一条记录聚焦第一行的按钮。

    if(ReferenceDossiers.FirstOrDefault() != null)
                        ReferenceDossiers.First().Selected = true;
    

    XAML

                         <DataGridTemplateColumn>
                                <DataGridTemplateColumn.CellStyle>
                                    <Style TargetType="DataGridCell">
                                        <Setter Property="IsTabStop" Value="False" />
                                        <Setter Property="BorderThickness" Value="0"/>                                        
                                    </Style>
                                </DataGridTemplateColumn.CellStyle>
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <Button behaviors:FocusExtension.IsFocused="{Binding Selected}"
                                             Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:MetroWindow}}, Path=DataContext.AddCommand}"
                                                CommandParameter="{Binding}">
                                            <Button.InputBindings>
                                                <KeyBinding Key="Enter" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:MetroWindow}}, Path=DataContext.AddCommand}"
                                                CommandParameter="{Binding}"/>
                                            </Button.InputBindings>
                                            <iconPacks:PackIconFontAwesome Kind="PlusCircle" Foreground="#FF94bf00"/>
                                        </Button>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
    

    要通过向上/向下按选择其他行的按钮,我们应该自定义单元格,如果不是,则 CellTemplate 焦点不是按钮,按回车键不起作用。