简体   繁体   English

为什么DataGrid不会通过ToolBar-Click失去焦点?

[英]Why DataGrid doesn't lose focus with ToolBar-Click?

I have a ToolBar with a Button and a DataGrid and a separate Button. 我有一个带有按钮的ToolBar和一个DataGrid和一个单独的Button。 在此处输入图片说明 When I edit a cell (EditMode) and then click the "separate" Button, the cell leaves EditMode and commit the changes. 当我编辑单元格(EditMode),然后单击“单独”按钮时,该单元格离开EditMode并提交更改。

But when I click the Button on the within the ToolBar, the cell doesn't leave EditMode and doesn't commit its changes. 但是,当我单击工具栏上的按钮时,单元格不会离开EditMode并且不会提交其更改。

How can I force the cell to commit its changes when I click the ToolBar Button? 单击工具栏按钮时,如何强制单元格提交其更改?

XAML: XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="DataContextSample" Height="543.538" Width="733.463"
        xmlns:self="clr-namespace:WpfApplication1">
    <DockPanel Margin="15">
        <ToolBar DockPanel.Dock="Top" Height="40">
            <Button Width="150" Height="20">ToolBarBUtton</Button>
        </ToolBar>
        <Button DockPanel.Dock="Top" Height="20" Width="150">Button</Button>
        <DataGrid DockPanel.Dock="Top" Name="DataGridJobConfigurations" ItemsSource="{Binding}"  VerticalScrollBarVisibility="Auto">
        </DataGrid>
    </DockPanel>
</Window>

C#: C#:

namespace WpfApplication1
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();

            List<Data> dataList = new List<Data>();

            dataList.Add(new Data(1));
            dataList.Add(new Data(2));
            dataList.Add(new Data(3));
            dataList.Add(new Data(4));

            this.DataContext = dataList;
        }
    }

    class Data {
        public Data(int i) {
            Number = i;
        }
        public int Number { get; set; }
    }
}

You could set the attached FocusManager.IsFocusScope property of the Toolbar to false : 您可以将Toolbar的附加FocusManager.IsFocusScope属性设置为false

<ToolBar DockPanel.Dock="Top" Height="40" FocusManager.IsFocusScope="False">
    <Button Width="150" Height="20">ToolBarBUtton</Button>
</ToolBar>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM