我有一个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;
}
答案 0 :(得分:2)
是的,ActualWidth
你需要寻找。但是自window is not rendered yet
以来,窗口的构造函数不是获取它的正确位置。而是使用Loaded
事件 -
public DatabaseTableWindow(string tableName, DataTable fields, string primaryKey)
{
InitializeComponent();
this.tableName = tableName;
this.fields = fields;
this.primaryKey = primaryKey;
lblTableName.Content = tableName;
Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
double x = this.ActualWidth;
}
修改强>
代替Loaded
,尝试在构造函数中挂钩ContentRendered
事件 -
this.ContentRendered += new EventHandler(MainWindow_ContentRendered);
void MainWindow_ContentRendered(object sender, EventArgs e)
{
double x = this.ActualWidth;
}
答案 1 :(得分:1)
假设您正在使用WPF Multiple Document Interface:
如果它是MdiChild你需要进行宽度属性计算,那么你可以在将它添加到Container之前绑定到MdiChild对象的Loaded-Event。
var mdiChild = new MdiChild
{
Title = "Window Using Code",
Content = new ExampleControl(),
Width = 714,
Height = 734,
Position = new Point(200, 30)
};
mdiChild.Loaded += new RoutedEventHandler(mdiChild_Loaded);
Container.Children.Add(mdiChild);
void mdiChild_Loaded(object sender, RoutedEventArgs e)
{
if (sender is MdiChild)
{
var width = (sender as MdiChild).Width;
Debug.WriteLine("Width: {0}", width);
}
}
答案 2 :(得分:1)
正如我们在WPF会议室里就这个问题聊天一样,这对于看似简单的问题来说是一个复杂的问题。
如果我们在mdiChild上设置宽度/高度,我们最终会失去调整大小功能。所以我已根据我们的chat
发布了一个似乎解决了这个问题的修补程序的要点所以为了得到大小限制,我使用了普通绑定(来自我的测试项目的这个复制粘贴):
<Window x:Class="TestApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:MDI="clr-namespace:WPF.MDI;assembly=WPF.MDI" xmlns:TestApplication="clr-namespace:TestApplication"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Title="MainWindow">
<MDI:MdiContainer Name="mainContainer">
<MDI:MdiChild x:Name="mdiChild" MaximizeBox="False" MinimizeBox="False" Resizable="True" ShowIcon="False" Width="{Binding Width, ElementName=childElement}" Height="{Binding Height, ElementName=childElement}">
<TestApplication:DatabaseTable x:Name="childElement"/>
</MDI:MdiChild>
</MDI:MdiContainer>
</Window>
这解决了主宽度和宽度高度问题。
这段代码需要替换WPF.MDI库中的MdiChild.cs Changeset 81799 resize处理程序源(您可以用此替换现有的相关代码):
/// <summary>
/// Handles the DragDelta event of the ResizeLeft control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Controls.Primitives.DragDeltaEventArgs"/> instance containing the event data.</param>
private void ResizeLeft_DragDelta(object sender, DragDeltaEventArgs e)
{
if (ActualWidth - e.HorizontalChange < MinWidth)
return;
double newLeft = e.HorizontalChange;
if (Position.X + newLeft < 0)
newLeft = 0 - Position.X;
Width = ActualWidth - newLeft;
Position = new Point(Position.X + newLeft, Position.Y);
if (sender != null)
Container.InvalidateSize();
}
/// <summary>
/// Handles the DragDelta event of the ResizeTop control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Controls.Primitives.DragDeltaEventArgs"/> instance containing the event data.</param>
private void ResizeTop_DragDelta(object sender, DragDeltaEventArgs e)
{
if (ActualHeight - e.VerticalChange < MinHeight)
return;
double newTop = e.VerticalChange;
if (Position.Y + newTop < 0)
newTop = 0 - Position.Y;
Height = ActualHeight - newTop;
Position = new Point(Position.X, Position.Y + newTop);
if (sender != null)
Container.InvalidateSize();
}
/// <summary>
/// Handles the DragDelta event of the ResizeRight control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Controls.Primitives.DragDeltaEventArgs"/> instance containing the event data.</param>
private void ResizeRight_DragDelta(object sender, DragDeltaEventArgs e)
{
if (ActualWidth + e.HorizontalChange < MinWidth)
return;
Width = ActualWidth + e.HorizontalChange;
if (sender != null)
Container.InvalidateSize();
}
/// <summary>
/// Handles the DragDelta event of the ResizeBottom control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Controls.Primitives.DragDeltaEventArgs"/> instance containing the event data.</param>
private void ResizeBottom_DragDelta(object sender, DragDeltaEventArgs e)
{
if (ActualHeight + e.VerticalChange < MinHeight)
return;
Height = ActualHeight + e.VerticalChange;
if (sender != null)
Container.InvalidateSize();
}
答案 3 :(得分:0)
尝试窥探窗口并遍历树以查看值