【发布时间】:2012-10-21 08:34:02
【问题描述】:
我有一个 WPF 窗口,我正在尝试获取 Width 属性。无论我尝试在哪里显示代码,它总是返回为NaN。我在互联网上查找并读到我实际上应该使用ActualWidth,但无论如何这都会返回 0。
如何摆脱滞后并获得窗口宽度的实际值?
XAML:
<Window x:Name="TableWindow" x:Class="QueryBuilder.DatabaseTable"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowStyle="None" Background="Transparent" SizeToContent="WidthAndHeight" ResizeMode="CanResize">
<Grid>
<DockPanel>
<StackPanel Name="titleBar" DockPanel.Dock="Top" Height="28" FlowDirection="RightToLeft" Orientation="Horizontal" Background="AliceBlue">
<Button x:Name="btnClose" Margin="0,0,5,0" Click="btnClose_Click">
</Button>
<Button>
</Button>
<Button>
</Button>
<Button x:Name="btnAll">ALL</Button>
<Label Name="lblTableName" FontSize="15" Margin="50,0,0,0"></Label>
</StackPanel>
<StackPanel Orientation="Vertical" Name="spFields">
</StackPanel>
</DockPanel>
</Grid>
</Window>
XAML.cs:
public DatabaseTableWindow(string tableName, DataTable fields, string primaryKey)
{
InitializeComponent();
this.tableName = tableName;
this.fields = fields;
this.primaryKey = primaryKey;
lblTableName.Content = tableName;
double x = this.ActualWidth;
}
【问题讨论】: