WPF控件随窗体大宽度高度改变而改变

前台控件中:

Width="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=Width}"

后台代码:

定义在加载事件里面this.SizeChanged += ProductData_SizeChanged;

private void ProductData_SizeChanged(object sender, SizeChangedEventArgs e)

{

CalculatedControlWidth(this.ActualWidth, this.ActualHeight);

}

private void CalculatedControlWidth(double windowWidth, double windowHeight)

{

double width = windowWidth;

double textBoxWidth = (width - 190) / 8.5;

productCode.Width = textBoxWidth;

typeCombox.Width = textBoxWidth;

double height = windowHeight;

dataGrid.Height = height - 232;

}