如何在WPF中找到UserControl宽度?

时间:2012-04-25 17:56:25

标签: c# wpf user-controls

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

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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