【问题标题】:How to get dimensions of WrapPanel defined in XAML如何获取在 XAML 中定义的 WrapPanel 的尺寸
【发布时间】: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
  • &lt;Window x:Class="HomeSecurity.MainWindow" ... Loaded="Window_Loaded"&gt; 和代码private void Window_Loaded(object sender, RoutedEventArgs e) { ... }
  • Window_Loaded 中调用createGUI 而不是构造函数,看看是否有帮助

标签: c# .net wpf xaml wrappanel


【解决方案1】:

要获得实际大小,您应该使用FrameworkElementActualWidthActualHeight 属性,但不要在构造函数中使用FrameworkElement.Loaded 事件:

<Window x:Class="HomeSecurity.MainWindow" ... Loaded="Window_Loaded"> 

在代码中

private void Window_Loaded(object sender, RoutedEventArgs e) 
{ 
   createGUI();
}

【讨论】:

    【解决方案2】:

    试试这个

     <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*" />
            <RowDefinition x:Name="Row"  Height="9*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition x:Name="Column" Width="2*" />
            <ColumnDefinition  Width="8*" />
        </Grid.ColumnDefinitions>
    
        <!--Used for calculating height and width-->
        <Grid x:Name="Dimesiongrid" Grid.Column="1" Grid.Row="2"></Grid>
    
        <ScrollViewer Grid.Column="1" Grid.Row="2">
            <WrapPanel x:Name="VideoPanel" />
        </ScrollViewer>
    </Grid>
    

    后面的代码

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            VideoPanel.Height = Dimesiongrid.ActualHeight;
            VideoPanel.Width = Dimesiongrid.ActualWidth;
        }
    

    这个高度和宽度你可以用来计算

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-12
      • 2011-01-02
      • 2014-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-24
      • 1970-01-01
      相关资源
      最近更新 更多