首页 > 解决方案 > WPF - 确定动态创建的控件是否适合容器

问题描述

动态创建控件有没有办法检查它是否适合像网格列这样指定的容器MaxWidth

我目前的解决方案是将控件添加到容器中,等待布局更新然后检查该控件的总宽度是否大于容器的宽度。

//Code inside Window's metod

var checkbox = new CheckBox
{
    Content = "Some content"
};
ButtonBarOption.Children.Add(checkbox);

UpdateLayout();

if (ButtonBarOption.ActualWidth > ButtonBarOptionColumn.MaxWidth)
{
    ButtonBarOption.Children.Remove(checkbox);
    OptionsContainer.Children.Add(checkbox);
}

就像上面的例子一样,当控件不适合容器时,我将它重新定位到不同的地方。

标签: wpf

解决方案


推荐阅读