测量在WPF中在运行时创建的控件

Nic*_*ell 15 c# data-binding wpf styles controltemplate

我知道这是一个很受欢迎的问题,但我找不到任何可以回答的问题,但如果我在搜索中遗漏了一些内容,我会道歉.

我正在尝试使用以下代码在运行时创建并测量控件(然​​后测量将用于选框样式滚动控件 - 每个控件的大小不同):

Label lb = new Label();
lb.DataContext= task;
Style style = (Style)FindResource("taskStyle");
lb.Style = style;

cnvMain.Children.Insert(0,lb);

width = lb.RenderSize.Width;
width = lb.ActualWidth;
width = lb.Width;
Run Code Online (Sandbox Code Playgroud)

代码创建一个Label控件并为其应用样式.该样式包含我的控件模板,该模板绑定到对象"任务".当我创建项目时它看起来很完美,但是当我尝试使用上面的任何属性测量控件时,我得到以下结果(我逐步检查并依次检查每个属性):

lb.Width = NaN
lb.RenderSize.Width = 0
lb.ActualWidth = 0
Run Code Online (Sandbox Code Playgroud)

有没有办法获得您在运行时创建的控件的渲染高度和宽度?

更新:

很抱歉取消选择您的解决方案作为答案.他们在我设置的基本示例系统上工作,但似乎没有完整的解决方案.

我觉得它可能与风格有关,所以很抱歉这个烂摊子,但我在这里粘贴整个东西.

一,资源:

    <Storyboard x:Key="mouseOverGlowLeave">
        <DoubleAnimation From="8" To="0" Duration="0:0:1" BeginTime="0:0:2" Storyboard.TargetProperty="GlowSize" Storyboard.TargetName="Glow"/>
    </Storyboard>

    <Storyboard x:Key="mouseOverTextLeave">
        <ColorAnimation From="{StaticResource buttonLitColour}" To="Gray" Duration="0:0:3" Storyboard.TargetProperty="Color" Storyboard.TargetName="ForeColour"/>
    </Storyboard>

    <Color x:Key="buttonLitColour" R="30" G="144" B="255" A="255" />

    <Storyboard x:Key="mouseOverText">
        <ColorAnimation From="Gray" To="{StaticResource buttonLitColour}" Duration="0:0:1" Storyboard.TargetProperty="Color" Storyboard.TargetName="ForeColour"/>
    </Storyboard>
Run Code Online (Sandbox Code Playgroud)

风格本身:

    <Style x:Key="taskStyle" TargetType="Label">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Canvas x:Name="cnvCanvas">
                        <Border  Margin="4" BorderBrush="Black" BorderThickness="3" CornerRadius="16">
                            <Border.Background>
                                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                    <GradientStop Offset="1" Color="SteelBlue"/>
                                    <GradientStop Offset="0" Color="dodgerBlue"/>
                                </LinearGradientBrush>
                            </Border.Background>
                            <DockPanel Margin="8">
                                <DockPanel.Resources>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="FontFamily" Value="corbel"/>
                                        <Setter Property="FontSize" Value="40"/>
                                    </Style>
                                </DockPanel.Resources>
                                <TextBlock VerticalAlignment="Center" Margin="4" Text="{Binding Path=Priority}"/>
                                <DockPanel DockPanel.Dock="Bottom">
                                    <Border Margin="4" BorderBrush="SteelBlue" BorderThickness="1" CornerRadius="4">
                                        <TextBlock Margin="4" DockPanel.Dock="Right" Text="{Binding Path=Estimate}"/>
                                    </Border>
                                    <Border Margin="4" BorderBrush="SteelBlue" BorderThickness="1" CornerRadius="4">
                                        <TextBlock Margin="4" DockPanel.Dock="Left" Text="{Binding Path=Due}"/>
                                    </Border>
                                </DockPanel>
                                <DockPanel DockPanel.Dock="Top">
                                    <Border Margin="4" BorderBrush="SteelBlue" BorderThickness="1" CornerRadius="4">
                                        <TextBlock  Margin="4" Foreground="LightGray" DockPanel.Dock="Left" Text="{Binding Path=List}"/>
                                    </Border>
                                    <Border Margin="4" BorderBrush="SteelBlue" BorderThickness="1" CornerRadius="4">
                                        <TextBlock Margin="4" Foreground="White" DockPanel.Dock="Left" Text="{Binding Path=Name}"/>
                                    </Border>
                                </DockPanel>
                            </DockPanel>
                        </Border>
                    </Canvas>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
Run Code Online (Sandbox Code Playgroud)

另外,我正在使用的代码:

Label lb = new Label(); //the element I'm styling and adding
  lb.DataContext= task; //set the data context to a custom class
  Style style = (Style)FindResource("taskStyle"); //find the above style
  lb.Style = style;

  cnvMain.Children.Insert(0,lb); //add the style to my canvas at the top, so other overlays work
  lb.UpdateLayout(); //attempt a full layout update - didn't work
  Dispatcher.Invoke(DispatcherPriority.Render, new Action(() =>
  {
      //Synchronize on control rendering
      double width = lb.RenderSize.Width;
      width = lb.ActualWidth;
      width = lb.Width;
  })); //this didn't work either
  lb.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); //nor did this
  double testHeight = lb.DesiredSize.Height;
  double testWidth = lb.RenderSize.Width; //nor did this
  width2 = lb.ActualWidth; //or this
  width2 = lb.Width; //or this

//positioning for my marquee code - position off-screen to start with
  Canvas.SetTop(lb, 20);
  Canvas.SetTop(lb, -999);

//tried it here too, still didn't work
  lb.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  testHeight = lb.DesiredSize.Height;
//add my newly-created label to a list which I access later to operate the marquee
  taskElements.AddLast(lb);
//still didn't work
  lb.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  testHeight = lb.DesiredSize.Height;
//tried a mass-measure to see if that would work - it didn't
  foreach (UIElement element in taskElements)
  {
      element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  }
Run Code Online (Sandbox Code Playgroud)

每次调用其中任何一个时,值都返回0或不是数字,但是当标签被渲染时,它显然具有可见的大小.当标签在屏幕上可见时,我尝试从按下按钮运行此代码,但产生相同的结果.

是因为我使用的风格?数据绑定也许?我做错了是不是很明显,还是WPF Gremlins 真的很讨厌我?

第二次更新:

进一步搜索后,我发现Measure()仅适用于原始元素大小.如果控件模板通过实际添加控件来修改它,那么最好的方法可能是测量每一个,但我承认这不仅有点凌乱.

编译器必须有某种方法来测量控件的所有内容,因为它必须使用它来放置项目,例如堆栈面板.必须有一些方法来访问它,但现在我完全没有想法.

Joe*_*ite 24

在WPF执行布局传递之前,控件将没有大小.我认为这是异步发生的.如果你在构造函数中这样做,你可以挂钩Loaded事件 - 那时布局就会发生,你在构造函数中添加的任何控件都会被调整大小.

但是,另一种方法是让控件计算它想要的大小.为此,您调用Measure,并将其传递给建议的大小.在这种情况下,您希望传递无限大小(意味着控件可以像它喜欢的那样大),因为这是Canvas在布局传递中要做的事情:

lb.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
Debug.WriteLine(lb.DesiredSize.Width);
Run Code Online (Sandbox Code Playgroud)

对Measure的调用实际上并不会更改控件的Width,RenderSize或ActualWidth.它只是告诉Label计算它想要的大小,并将该值放入DesiredSize(一个属性,其唯一目的是保存最后一次Measure调用的结果).


Nic*_*ell 1

解决了!

问题出在我的 XAML 中。在我的标签模板的最高级别,有一个没有高度或宽度字段的父画布。由于它不必为其子级修改其大小,因此它始终设置为 0,0。通过删除它并用边框替换根节点(边框必须调整大小以适合其子节点),高度和宽度字段将被更新并传播回 Measure() 调用时的代码。