Pregunta

I have WrapPanel defined in XAML:

<ScrollViewer Grid.Column="1" Grid.Row="2">
            <WrapPanel x:Name="VideoPanel" >
  </WrapPanel>
        </ScrollViewer>

And then when I access the size of that WrapPanel in .cs

  Console.WriteLine(VideoPanel.ActualWidth + " x " + VideoPanel.ActualHeight);
        Console.WriteLine(VideoPanel.Width + " x " + VideoPanel.Height);

I get the output:

   0 x 0
0 x 0
is not a number x is not a number

How to get actual size?

Full 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>

Full code:

    /// <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);

        }
    }
¿Fue útil?

Solución

To get actual size you should use ActualWidth and ActualHeight properties of FrameworkElement but instead of getting them in constructor use FrameworkElement.Loaded event:

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

and in code

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

Otros consejos

try this

 <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>

and behind code

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

this height and width you can use for calculation

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top