WPF:总是触发 SizeChanged
WPF : the SizeChanged always being fired
我使用 ResponsiveGrid 制作动态页面并设置 SizeChanged 事件以更改按钮的位置。但是 SizeChanged 总是被触发使 windows 闪烁,并且 SizeChanged 事件无法停止,这是我在 .cs
中的代码
private void Condition_SizeChanged(object sender, EventArgs e)
{
if (ActualWidth > 1480)
{
Col1.Height = new GridLength(100.0, GridUnitType.Star);
Col2.Height = new GridLength(1.0, GridUnitType.Star);
Row1.Width = new GridLength(5.0, GridUnitType.Star);
Row2.Width = new GridLength(1.0, GridUnitType.Star);
CommonButtonSearch.SetValue(Grid.ColumnProperty, 1);
CommonButtonSearch.SetValue(Grid.RowProperty, 0);
CommonButtonSearch.Margin = new Thickness(30, (ActualHeight - 32) / 2, 0, (ActualHeight - 32) / 2);
}
else
{
Row1.Width = new GridLength(100.0, GridUnitType.Star);
Row2.Width = new GridLength(1.0, GridUnitType.Star);
Col1.Height = new GridLength(5.0, GridUnitType.Star);
Col2.Height = new GridLength(1.0, GridUnitType.Star);
CommonButtonSearch.SetValue(Grid.ColumnProperty, 0);
CommonButtonSearch.SetValue(Grid.RowProperty, 1);
CommonButtonSearch.Margin = new Thickness((ActualWidth - 120) / 2, 30, (ActualWidth - 120) / 2, 30);
}
}
每当更改大小时都会引发该事件,这可能在您在代码中设置行和列的 Width
和 Height
属性时发生。
您可能希望在执行代码时使用变量来“暂停”引发事件:
private bool _handle = true;
private void Condition_SizeChanged(object sender, EventArgs e)
{
if (!_handle)
return;
_handle = false;
if (ActualWidth > 1480)
{
Col1.Height = new GridLength(100.0, GridUnitType.Star);
Col2.Height = new GridLength(1.0, GridUnitType.Star);
Row1.Width = new GridLength(5.0, GridUnitType.Star);
Row2.Width = new GridLength(1.0, GridUnitType.Star);
CommonButtonSearch.SetValue(Grid.ColumnProperty, 1);
CommonButtonSearch.SetValue(Grid.RowProperty, 0);
CommonButtonSearch.Margin = new Thickness(30, (ActualHeight - 32) / 2, 0, (ActualHeight - 32) / 2);
}
else
{
Row1.Width = new GridLength(100.0, GridUnitType.Star);
Row2.Width = new GridLength(1.0, GridUnitType.Star);
Col1.Height = new GridLength(5.0, GridUnitType.Star);
Col2.Height = new GridLength(1.0, GridUnitType.Star);
CommonButtonSearch.SetValue(Grid.ColumnProperty, 0);
CommonButtonSearch.SetValue(Grid.RowProperty, 1);
CommonButtonSearch.Margin = new Thickness((ActualWidth - 120) / 2, 30, (ActualWidth - 120) / 2, 30);
}
_handle = true;
}
我使用 ResponsiveGrid 制作动态页面并设置 SizeChanged 事件以更改按钮的位置。但是 SizeChanged 总是被触发使 windows 闪烁,并且 SizeChanged 事件无法停止,这是我在 .cs
中的代码 private void Condition_SizeChanged(object sender, EventArgs e)
{
if (ActualWidth > 1480)
{
Col1.Height = new GridLength(100.0, GridUnitType.Star);
Col2.Height = new GridLength(1.0, GridUnitType.Star);
Row1.Width = new GridLength(5.0, GridUnitType.Star);
Row2.Width = new GridLength(1.0, GridUnitType.Star);
CommonButtonSearch.SetValue(Grid.ColumnProperty, 1);
CommonButtonSearch.SetValue(Grid.RowProperty, 0);
CommonButtonSearch.Margin = new Thickness(30, (ActualHeight - 32) / 2, 0, (ActualHeight - 32) / 2);
}
else
{
Row1.Width = new GridLength(100.0, GridUnitType.Star);
Row2.Width = new GridLength(1.0, GridUnitType.Star);
Col1.Height = new GridLength(5.0, GridUnitType.Star);
Col2.Height = new GridLength(1.0, GridUnitType.Star);
CommonButtonSearch.SetValue(Grid.ColumnProperty, 0);
CommonButtonSearch.SetValue(Grid.RowProperty, 1);
CommonButtonSearch.Margin = new Thickness((ActualWidth - 120) / 2, 30, (ActualWidth - 120) / 2, 30);
}
}
每当更改大小时都会引发该事件,这可能在您在代码中设置行和列的 Width
和 Height
属性时发生。
您可能希望在执行代码时使用变量来“暂停”引发事件:
private bool _handle = true;
private void Condition_SizeChanged(object sender, EventArgs e)
{
if (!_handle)
return;
_handle = false;
if (ActualWidth > 1480)
{
Col1.Height = new GridLength(100.0, GridUnitType.Star);
Col2.Height = new GridLength(1.0, GridUnitType.Star);
Row1.Width = new GridLength(5.0, GridUnitType.Star);
Row2.Width = new GridLength(1.0, GridUnitType.Star);
CommonButtonSearch.SetValue(Grid.ColumnProperty, 1);
CommonButtonSearch.SetValue(Grid.RowProperty, 0);
CommonButtonSearch.Margin = new Thickness(30, (ActualHeight - 32) / 2, 0, (ActualHeight - 32) / 2);
}
else
{
Row1.Width = new GridLength(100.0, GridUnitType.Star);
Row2.Width = new GridLength(1.0, GridUnitType.Star);
Col1.Height = new GridLength(5.0, GridUnitType.Star);
Col2.Height = new GridLength(1.0, GridUnitType.Star);
CommonButtonSearch.SetValue(Grid.ColumnProperty, 0);
CommonButtonSearch.SetValue(Grid.RowProperty, 1);
CommonButtonSearch.Margin = new Thickness((ActualWidth - 120) / 2, 30, (ActualWidth - 120) / 2, 30);
}
_handle = true;
}