【发布时间】:2014-01-08 13:10:59
【问题描述】:
我在 XAML 中定义了WrapPanel:
<ScrollViewer Grid.Column="1" Grid.Row="2">
<WrapPanel x:Name="VideoPanel" >
</WrapPanel>
</ScrollViewer>
然后当我在.cs 中访问WrapPanel 的大小时
Console.WriteLine(VideoPanel.ActualWidth + " x " + VideoPanel.ActualHeight);
Console.WriteLine(VideoPanel.Width + " x " + VideoPanel.Height);
我得到了输出:
0 x 0
0 x 0
is not a number x is not a number
如何获得实际尺寸?
完整的 XAML:
<Window x:Class="HomeSecurity.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:HomeSecurity"
Title="MainWindow" WindowState="Maximized">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="9*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="8*" />
</Grid.ColumnDefinitions>
<ScrollViewer Grid.Column="1" Grid.Row="2">
<WrapPanel x:Name="VideoPanel" >
</WrapPanel>
</ScrollViewer>
</Grid>
</Window>
完整代码:
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
createGUI();
}
private void createGUI() {
AddVideoStream("192.168.0.3");
}
private void AddVideoStream(String sourceIP) {
Console.WriteLine(VideoPanel.ActualWidth + " x " + VideoPanel.ActualHeight);
Console.WriteLine(VideoPanel.Width + " x " + VideoPanel.Height);
}
}
【问题讨论】:
-
你试过
ActualWidth/ActualHeight吗? -
你什么时候买这个尺寸的?是不是在构造函数中?
-
所以是的,在构造函数中。尝试在
Window.Loaded事件中调用createGUI -
<Window x:Class="HomeSecurity.MainWindow" ... Loaded="Window_Loaded">和代码private void Window_Loaded(object sender, RoutedEventArgs e) { ... } -
在
Window_Loaded中调用createGUI而不是构造函数,看看是否有帮助
标签: c# .net wpf xaml wrappanel