如何在WPF中查找UserControl的宽度?

5
<UserControl x:Class="JIMS.View.Settings.Settings"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    Name="SettingsWindow">       
    <Border Style="{StaticResource WindowBorderStyle}" Height="100">    
    <StackPanel Orientation="Vertical">
        <my:TitleBar Title="settings"/>
        <StackPanel Orientation="Horizontal">
            <StackPanel Orientation="Horizontal">
                <StackPanel Orientation="Vertical" Margin="10,0,0,0">
                    <Label>Theme :</Label>                        
                </StackPanel>
                <StackPanel Orientation="Vertical" Width="200" Margin="0,0,10,0">                        
                    <ComboBox Name="cmbTheme" ItemsSource="{Binding Path=Themes}" ></ComboBox>                        
                </StackPanel>
            </StackPanel>
        </StackPanel>
    </StackPanel>        
</Border>
</UserControl>

这是我的UserControl,我没有设置其宽度和高度属性。但是在一些代码后台中,我想获取这个UserControl的高度和宽度,但我无法得到它们。

double width=uctrl.Width;

当我执行此操作时,它会返回NaN

double width=ctrl.ActualWidth;

给我返回 0

我需要宽度和高度的代码

private void OpenChild(UserControl ctrl)
{
    bool alreadyExist = false;
    ctrl.Uid = ctrl.Name;
    foreach (UIElement child in JIMSCanvas.Children)
    {
        if (child.Uid == ctrl.Uid)
        {
            alreadyExist = true;
            Canvas.SetZIndex(child, GetMaxZIndex);
        }
    }
    if (!alreadyExist)
    {
        JIMSCanvas.Children.Add(ctrl);
            JIMSCanvas.InvalidateMeasure();
        double top = (JIMSCanvas.ActualHeight - ctrl.Height) / 2;
        double left = (JIMSCanvas.ActualWidth - ctrl.Width) / 2;
        Canvas.SetLeft(ctrl, left);
        Canvas.SetTop(ctrl, top);
    }
}

什么是JIMSCanvas?它可能还没有被“布局”,因此还没有宽度吗? - erikH
JIMSCanvas是我的自定义画布。 - Kishore Kumar
2个回答

2
在一个全新的项目中,我写了以下代码(当改变窗口大小时,它会给我更新的宽度):
<Window xmlns:my="clr-namespace:WpfApplication2"
        x:Class="WpfApplication2.Window32"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window32"
        Height="300"
        Width="300">
  <Grid>
    <my:UserControl3 x:Name="uc3" />
    <TextBlock Height="23"
               HorizontalAlignment="Left"
               Margin="126,121,0,0"
               Name="textBlock1"
               Text="{Binding ElementName=uc3, Path=ActualWidth}"
               VerticalAlignment="Top" />
  </Grid>
</Window>

3
嘿,那个 -1 很快就来了...根据你未编辑的帖子,我不知道你没有使用 Windows。如果你没有提供足够的信息,就不要贬低它。在查询 ActualWidth / ActualHeight 之前尝试执行 JIMSCanvas.InvalidateMeasure()。 - NestorArturo
只有在两行代码之间有消息框时,InvalidateMeasure()才会起作用。 - Kishore Kumar

0

我用这段代码解决了这个问题...

private void OpenChild(UserControl ctrl)
{
    bool alreadyExist = false;
    ctrl.Uid = ctrl.Name;
    foreach (UIElement child in JIMSCanvas.Children)
    {
        if (child.Uid == ctrl.Uid)
        {
            alreadyExist = true;
            Canvas.SetZIndex(child, GetMaxZIndex);
        }
    }
    if (!alreadyExist)
    {
        JIMSCanvas.Children.Add(ctrl);
        JIMSCanvas.UpdateLayout();
        double top = (JIMSCanvas.ActualHeight - ctrl.ActualHeight) / 2;
        double left = (JIMSCanvas.ActualWidth - ctrl.ActualWidth) / 2;
        Canvas.SetLeft(ctrl, left);
        Canvas.SetTop(ctrl, top);                               
    }
}

我使用了 JIMSCanvas.UpdateLayout();

感谢大家的帮助,特别是 @NestorArturo


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接