wpf 控件宽度绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5873007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:50:27  来源:igfitidea点击:

wpf control width binding

wpfbindingwidth

提问by Terco

I have two borders with content, The second border width changes depending on the content, I'm trying to bind the first border to the second border width but it's not working and I'm not sure what i'm missing. Can someone give me some direction please? Below is a example of what I'm currently trying.

我有两个带有内容的边框,第二个边框宽度根据内容而变化,我试图将第一个边框绑定到第二个边框宽度,但它不起作用,我不确定我错过了什么。有人可以给我一些指导吗?以下是我目前正在尝试的示例。

<Border x:Name="border1" Width="{Binding Path=Width, ElementName=border2}">
    ... 
</Border>

<Border x:Name="border2">
    ...
</Border>

回答by H.B.

Change the path to ActualWidth.

将路径更改为ActualWidth.

回答by FatemehEbrahimiNik

change path to ViewportWidth too !

也将路径更改为 ViewportWidth !

<Grid x:Name="mainGrid">
//and some elements exist there
  <Grid> 
    <Border x:Name="border_btn" Width="{Binding ElementName=root ,Path=ViewportWidth, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>  
  </Grid>
</Grid>

use ViewportWidth instead of ActualWidth because in this case ActualWidth is always shown 0! for test this problem you can test this with this line in your xaml.

使用 ViewportWidth 而不是 ActualWidth 因为在这种情况下 ActualWidth 始终显示为 0!为了测试这个问题,你可以在你的 xaml 中用这一行来测试这个问题。

  <TextBlock Text="{Binding  ElementName=mainGrid, Path=ActualWidth ,Mode=TwoWay}" Foreground="Red"/>