I’ve got a WPF window, and I’m trying to get the Width property. No matter where I try to present the code, it always returns as NaN. I have looked up on the internet and read that I should actually be using ActualWidth instead, but this returns 0 no matter what.
How can I get rid of the lag and get an actual value for my window’s width?
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;
}
As we’ve been chatting in the WPF room regarding this question, this was a compounded problem to seemingly easy thing.
If we set width / height on mdiChild, we end up losing resize functionality. so I’ve posted a gist with the fix that seems to have solved this issue as per our chat
so to get the size bound, I used normal bindings (this copy paste from my test project):
this solves the primary width & height issue.
This piece of code needs to replace the MdiChild.cs Changeset 81799 resize handler source in WPF.MDI library (you can replace the existing relevant code in there with this):