简体   繁体   中英

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

I have a ToolBar with a Button and a DataGrid and a separate Button. 在此处输入图片说明 When I edit a cell (EditMode) and then click the "separate" Button, the cell leaves EditMode and commit the changes.

But when I click the Button on the within the ToolBar, the cell doesn't leave EditMode and doesn't commit its changes.

How can I force the cell to commit its changes when I click the ToolBar Button?

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#:

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 DockPanel.Dock="Top" Height="40" FocusManager.IsFocusScope="False">
    <Button Width="150" Height="20">ToolBarBUtton</Button>
</ToolBar>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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