Silverlight/WPF:在屏幕上渲染 UIElement 后检索其大小

发布于 2024-09-03 02:44:52 字数 729 浏览 1 评论 0原文

我有以下简单的代码:

            var canvas = new Canvas();

            foreach (var ztring in strings)
            {
                var textblock = new TextBlock();
                textblock.Text = ztring;

                panel.Children.Add(textblock);

                textblock.Measure(infiniteSize);
            }

此时,我希望任何大小属性(高度/宽度、ActualHeight/ActualWidth、DesiredSize、RenderSize) 为我提供文本块的大小。他们都没有。

无论字体大小如何,ActualHeight 始终给出 16.0ActualWidth 根据文本长度而不是字体大小而变化。

我更改父容器上的字体大小,而不是 TextBlock 本身。

我觉得我缺少一些理解代码隐藏中 silverlight 元素操作的基本元素。

问题是:如何获得 TextBlock实际像素大小

I have the following simple piece of code:

            var canvas = new Canvas();

            foreach (var ztring in strings)
            {
                var textblock = new TextBlock();
                textblock.Text = ztring;

                panel.Children.Add(textblock);

                textblock.Measure(infiniteSize);
            }

At this point I would expect any of the size properties (Height/Width, ActualHeight/ActualWidth, DesiredSize, RenderSize) to give me the size of the textblock. None of them do.

ActualHeight always gives 16.0 no matter what size font. ActualWidth changes according to the text length but not the font size.

I change the font size on the parent container and not the TextBlock itself.

I feel like I am missing some basic element of understanding the manipulation of silverlight elements from within the codebehind.

The question is: how do I get the real actual pixel size of my TextBlock?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

微暖i 2024-09-10 02:44:52

下面是一个使用代码隐藏将 TextBlock 添加到 Canvas 的示例,一旦呈现 TextBlock,它就会在标题中显示其高度窗户。这就是您要找的吗?

XAML:

<Window x:Class="HeightTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <StackPanel TextBlock.FontSize="30">
        <Canvas Name="_canvas" Height="200"/>
    </StackPanel>
</Window>

隐藏代码:

using System.Windows;
using System.Windows.Controls;

namespace HeightTest
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            TextBlock textBlock = new TextBlock();
            textBlock.Text = "Hello";
            Canvas.SetLeft(textBlock, 25);
            textBlock.Loaded += 
                (sender, e) => 
                {
                    Title = textBlock.ActualHeight.ToString();
                };
            _canvas.Children.Add(textBlock);
        }
    }
}

Below is a sample that adds a TextBlock to a Canvas using code behind and once the TextBlock is rendered, it displays its height in the title of the window. Is that what you are looking for?

XAML:

<Window x:Class="HeightTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <StackPanel TextBlock.FontSize="30">
        <Canvas Name="_canvas" Height="200"/>
    </StackPanel>
</Window>

Code behind:

using System.Windows;
using System.Windows.Controls;

namespace HeightTest
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            TextBlock textBlock = new TextBlock();
            textBlock.Text = "Hello";
            Canvas.SetLeft(textBlock, 25);
            textBlock.Loaded += 
                (sender, e) => 
                {
                    Title = textBlock.ActualHeight.ToString();
                };
            _canvas.Children.Add(textBlock);
        }
    }
}
晨敛清荷 2024-09-10 02:44:52

您是否尝试过使用像Grid这样的真实容器而不是Canvas
如果您尝试使用 Dispatcher.BeginInvoke 在 Measure 之后读取 ActualSize 属性会怎样?

Have you tried using a real container like a Grid instead of Canvas?
What if you try reading the ActualSize property after the Measure using a Dispatcher.BeginInvoke?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文