[英]WPF FrameworkElement in GridView - update column width
I have a GridView. 我有一个GridView。 In one column a CustomControl (derived from FrameworkElement) is in the DataTemplate.
在一列中,DataTemplate中有一个CustomControl(派生自FrameworkElement)。
According to some interaction the CustomControl will changed its Width. 根据某些交互,CustomControl将更改其宽度。 How can I achieve, that the GridViewColumn changes it's Width dynamically to the Width of the widest CustomControl?
如何实现GridViewColumn将其Width动态更改为最宽的CustomControl的Width?
Handle the SizeChanged
event of the control and set the Width
of the GridViewColumn
: 处理控件的
SizeChanged
事件并设置GridViewColumn
的Width
:
private void TextBox_SizeChanged(object sender, SizeChangedEventArgs e)
{
FrameworkElement ctrl = sender as FrameworkElement;
theColumn.Width = ctrl.ActualWidth + 20;
}
XAML: XAML:
<GridViewColumn x:Name="theColumn">
<GridViewColumn.CellTemplate>
<DataTemplate>
<local:TheResizingControl />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
The control nor the framework won't do this for you. 控件或框架不会为您完成此操作。 You need to write some code that does this.
您需要编写一些执行此操作的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.