在运行时获取组合框弹出窗口的大小

发布于 2024-11-01 15:45:43 字数 113 浏览 7 评论 0原文

我正在尝试获取 Silverlight ComboBox 下拉窗口的宽度和高度。 不幸的是 ActualWidthActualHeight 始终返回 0。

I am trying to get the width and height of a Silverlight ComboBox's dropdown window.
Unfortunately ActualWidth and ActualHeight are returning 0 always.

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

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

发布评论

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

评论(3

佼人 2024-11-08 15:45:43
<ComboBox x:Name="comboBox" Height="20" Width="120">
    <ComboBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel SizeChanged="StackPanel_SizeChanged"/>
        </ItemsPanelTemplate>
    </ComboBox.ItemsPanel>
</ComboBox>

private void StackPanel_SizeChanged(object sender, SizeChangedEventArgs e)
{
   var w=   e.NewSize.Width;
   var h=  e.NewSizeHeight;
}

但这不是一个好方法。

<ComboBox x:Name="comboBox" Height="20" Width="120">
    <ComboBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel SizeChanged="StackPanel_SizeChanged"/>
        </ItemsPanelTemplate>
    </ComboBox.ItemsPanel>
</ComboBox>

private void StackPanel_SizeChanged(object sender, SizeChangedEventArgs e)
{
   var w=   e.NewSize.Width;
   var h=  e.NewSizeHeight;
}

But it's not a good way.

堇年纸鸢 2024-11-08 15:45:43

如果不实际渲染弹出窗口,您就无法获得实际大小。这意味着如果弹出窗口隐藏,ActualSizes 将为 0。这是 WPF 为您执行布局和渲染逻辑的结果。

您可以通过执行 Measure< 来获取弹出窗口请求的高度/a> 传递弹出窗口本身。如果尚未创建弹出窗口,那么您仍然遇到麻烦。 (并且它可能直到第一次显示才被创建。)

You can't get the actual size without actually rendering the popup. This implies that ActualSizes will be 0 if the popup is hidden. This is a consequence of WPF performing layout and rendering logic for you.

You could possibly get the popup's requested height by performing a Measure pass on the popup itself. If the popup hasn't been created yet, you're still in trouble though. (And it may not get created until the first time it's displayed.)

感情旳空白 2024-11-08 15:45:43

我自己找到了一个解决方案:您必须在测量之前将弹出窗口的“IsOpen”设置为 true,然后将其设置回 false。这是我让它发挥作用的唯一方法。

I found a solution to this by myself: you have to set the popup's "IsOpen" to true before measuring, and then set it back to false. It's the only way I could make it work.

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