如何绑定控件的实际实际宽度(包括其边距)? [英] How to bind to a control's actually actual width (including its margins)?

查看:126
本文介绍了如何绑定控件的实际实际宽度(包括其边距)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据一些人实际宽度是使用ActualWidth 属性,如下面的示例所示。这是有道理的,但我似乎遇到矛盾的行为。

According to some folks, the actual width is obtained using ActualWidth attribute as shown in the example below. It makes sense but I seem to experience contradicting behavior.

<Canvas Width="{Binding ActualWidth,ElementName=Expy}">
  <Expander x:Name="Expy"
                    HorizontalAlignment="Left"
                    Margin="0,0,0,0"
                    VerticalAlignment="Top" ...>
  ...
  </Expander>
</Canvas>

在上面的设置中,行为与期望一致,尽管紧紧挤压在一起,包含画布的面板中的元素不会被它的前身重叠。

In the setup above, the behavior is consistent with the expectations and, although tightly squeezed together, the next element in the panel containing the canvas is not overlapped by it's predecessor.

但是,如果我将页边距更改一些,我可以清楚地看到画布侵入下一个元素,估计我在margin属性中请求的相同数量的pixies。所以看起来,实际宽度 不是实际的宽度,而是没有边距的宽度。

However, if I change the margins to a bit wider, I can clearly see that the canvas intrude on the next element, estimatingly by the same number of pixies that I requested in the margin attribute. So it'd appear that the ActualWidth isn't the actual width but the width without the margin.


  1. 我在这里混淆某些东西,如果是,那么什么?

  2. 如何获取并绑定到 actaully 实际的渲染宽度? / li>
  1. Am I confusing something here and if so, what?
  2. How to obtain and bind to the actaully actual, rendered width?

推荐答案

链接的答案说:


实际宽度帐户填充和边距...

ActualWidth accounts for padding and margins ...

这是不正确的。 ActualWidth 仅包含填充,而不是边距(与 ActualHeight 相同)。

This is incorrect. The ActualWidth includes only the padding, not the margin (same with ActualHeight).

其他人对该答案留下的评论提供了适当的更正。

A comment that has been left on that answer by somebody else provides the appropriate correction.

这个XAML代码说明了这个问题:

This XAML code illustrates the issue:

<Window x:Class="..."
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <TextBlock x:Name="First" Text="Some text" Padding="10" Margin="0,0"
            HorizontalAlignment="Left" Background="Yellow" />
        <TextBlock Text="{Binding ActualWidth, ElementName=First}" />

        <TextBlock x:Name="Second" Text="Some text" Padding="10" Margin="10,0"
            HorizontalAlignment="Left" Background="LimeGreen" />
        <TextBlock Text="{Binding ActualWidth, ElementName=Second}" />
    </StackPanel>
</Window>

如果你运行这个,你会看到两个一些文本文本块具有相同的 ActualWidth 值,尽管第二个应用了水平边距。

If you run this you will see that both of the "Some text" text blocks have the same ActualWidth value despite the second one having horizontal margins applied to it.

ActualWidth 属性不包括已经说明的边距。您可以做的是将边距应用于父元素(画布),而不是将其应用于扩展器。

You asked how you could take this into account. There is no way of doing this through binding because the ActualWidth property doesn't include the margin as already stated. What you can do is apply the margin to the parent element (the canvas) instead of applying it to the expander.

换句话说,而不是这样做:

In other words, instead of doing this:

<Canvas Width="{Binding ActualWidth,ElementName=Expy}">
    <Expander x:Name="Expy" ... Margin="10" ... >
        ...
    </Expander>
</Canvas>

执行此操作:

<Canvas Width="{Binding ActualWidth,ElementName=Expy}" Margin="10">
    <Expander x:Name="Expy" ... >
        ...
    </Expander>
</Canvas>

这篇关于如何绑定控件的实际实际宽度(包括其边距)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆