Подлагивает форма после вызова из menuItem WPF C#
Пишу приложение на WPF и столкнулся со следующей проблемой: запускаю окно при нажатии на кнопку, и все работает корректно. Но если запускать окно при нажатии на menuItem (или даже если при нажатии на menuItem вызывается метод обрабатывающий нажатие кнопки вызова окна), то форма подлагивает при поиске в таблице.
Код обработчиков событий:
private void Button_Click(object sender, RoutedEventArgs e)
{
Nomenclature_Click(sender, e);
}
private void Nomenclature_Click(object sender, RoutedEventArgs e)
{
if (this.nomenclature == null)
{
nomenclature = new Nomenclature(database);
nomenclature.Show();
}
else
nomenclature.Focus();
this.nomenclature.Closing += (Nomenclature, args) => this.nomenclature = null;
}
Код внутри окна:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace inventory_accounting
{
/// <summary>
/// Логика взаимодействия для Nomenclature.xaml
/// </summary>
public partial class Nomenclature : Window
{
public Database database;
public Nomenclature(Database database)
{
InitializeComponent();
this.database = database;
tableSettings();
addingEvents();
}
private void tableSettings()
{
table.IsReadOnly = true;
table.CanUserAddRows = false;
table.CanUserDeleteRows = false;
table.AutoGenerateColumns = false;
table.ItemsSource = database.Products;
}
private void addingEvents()
{
find_code.GotFocus += Find_GotFocus;
find_name.GotFocus += Find_GotFocus;
find_quantity.GotFocus += Find_GotFocus;
find_salePrice.GotFocus += Find_GotFocus;
find_purchasePrice.GotFocus += Find_GotFocus;
find_code.LostFocus += Find_LostFocus;
find_name.LostFocus += Find_LostFocus;
find_quantity.LostFocus += Find_LostFocus;
find_salePrice.LostFocus += Find_LostFocus;
find_purchasePrice.LostFocus += Find_LostFocus;
}
private void Find_GotFocus(object sender, RoutedEventArgs e)
{
if ((sender as TextBox).Name == "find_code")
find_code.Text = "";
if ((sender as TextBox).Name == "find_name")
find_name.Text = "";
if ((sender as TextBox).Name == "find_quantity")
find_quantity.Text = "";
if ((sender as TextBox).Name == "find_salePrice")
find_salePrice.Text = "";
if ((sender as TextBox).Name == "find_purchasePrice")
find_purchasePrice.Text = "";
}
private void Find_LostFocus(object sender, RoutedEventArgs e)
{
if ((sender as TextBox).Name == "find_code")
find_code.Text = "Код";
if ((sender as TextBox).Name == "find_name")
find_name.Text = "Наименование";
if ((sender as TextBox).Name == "find_quantity")
find_quantity.Text = "Количество";
if ((sender as TextBox).Name == "find_salePrice")
find_salePrice.Text = "Розница";
if ((sender as TextBox).Name == "find_purchasePrice")
find_purchasePrice.Text = "Закупка";
}
private void Find_name_TextChanged(object sender, TextChangedEventArgs e)
{
if (find_name == null || database == null)
return;
var tmp = database.Products.Where(x => x.Name.StartsWith(find_name.Text, StringComparison.OrdinalIgnoreCase)).ToList();
if (tmp.Count == 0)
{
return;
}
table.ScrollIntoView(tmp[0]);
table.SelectedIndex = table.Items.IndexOf(tmp[0]);
}
}
}
Разметка таблицы:
<DataGrid ScrollViewer.CanContentScroll="True" x:Name="table" AutoGenerateColumns="True" Width="auto" Margin="0,0,0,0" ColumnWidth="auto" FontSize="18" SelectionMode="Single" >
<DataGrid.Columns>
<DataGridTextColumn Header="Код" Binding="{Binding Code}" Width="auto" />
<DataGridTextColumn Header="Наименование" Binding="{Binding Name}" Width="auto" />
<DataGridTextColumn Header="Остатки" Binding="{Binding Quantity}" Width="*" />
<DataGridTextColumn Header="Закупка" Binding="{Binding PurchasePrice}" Width="*" />
<DataGridTextColumn Header="Розница" Binding="{Binding SalePrice}" Width="*" />
</DataGrid.Columns>
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True">
<Setter Property="Background" Value="#CCDAFF" />
<Setter Property="Foreground" Value="Black" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
</DataGrid>