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

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

问题描述

根据某些人实际宽度是使用 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属性中请求的像素数相同.因此,看来 ActualWidth 不是 actual 宽度,而是没有边距的宽度.

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

推荐答案

链接的答案说:

ActualWidth表示填充和边距...

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天全站免登陆