【问题标题】:XAML, binding Width and Height properties to the same properties of other controlXAML,将 Width 和 Height 属性绑定到其他控件的相同属性
【发布时间】:2011-01-10 07:58:39
【问题描述】:

我正在尝试创建反射效果,效果很好,只是我必须对一些值进行硬编码。这是我的 XAML:

 <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="60"/>
        <RowDefinition />
        <RowDefinition Height="80"/>
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="1" VerticalAlignment="Center">
        <UserControl x:Name="CurrentPresenter" />
        <Border Width="500" Height="200" >
            <Border.Background>
                <VisualBrush Visual="{Binding ElementName=CurrentPresenter}" >
                    <VisualBrush.Transform>
                        <TransformGroup>
                            <ScaleTransform ScaleX="1" ScaleY="-1" CenterX="500" CenterY="99" />
                        </TransformGroup>
                    </VisualBrush.Transform>
                </VisualBrush>
            </Border.Background>
            <Border.OpacityMask>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,0.6">
                        <GradientStop Offset="-0.6" Color="Black"></GradientStop>
                        <GradientStop Offset="0.6" Color="Transparent"></GradientStop>
                    </LinearGradientBrush>
                </Border.OpacityMask>
        </Border>
    </StackPanel>
</Grid>

我尝试将 Border 的 Width="500"Height="200" 替换为 Width="{Binding ElementName=CurrentPresenter, Path=Width}"Height="{Binding ElementName=CurrentPresenter, Path=Height}",但它似乎不起作用。

这段代码有什么问题?

更新:如果我在这里设置宽度和高度:

<UserControl x:Name="CurrentPresenter" Height="200" Width="500" />

它按预期工作。但是,如果我在 UserControl XAML 中设置这些值,它就不起作用。有什么想法吗?

【问题讨论】:

  • 当您说它不起作用时,您实际看到的是什么样的行为?边框是否显得太小,或者它只是无限延伸(我都见过)。
  • 它伸展以填满容器。
  • 如果您在 UserControl XAML 中设置了高度和宽度,那么 UserControl 是否看起来是正确的大小而只是边框在拉伸,还是两个控件的大小都错误?
  • 如果我在 UserControl XAML 中设置高度和宽度,UserControl 会以正确的大小显示,但边框会拉伸以填充容器。似乎如果我没有明确设置宽度和高度值,则绑定不起作用。我将通过在用户控件中创建“反射效果”来解决此问题,但我只是好奇为什么这不能按预期工作。

标签: xaml binding user-controls height width


【解决方案1】:

您是否尝试过将BorderHeightWidth 属性绑定到UserControl 的ActualHeightActualWidth 属性,而不是绑定到UserControl 的Height 和Width 属性?

<Border 
 Width="{Binding ElementName=CurrentPresenter, Path=ActualWidth}"
 Height="{Binding ElementName=CurrentPresenter, Path=ActualHeight}" >

【讨论】:

  • 这个修复节省了一些白发。谢谢塔比。
【解决方案2】:

更改 ViewportWidth 的路径!它为我工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    相关资源
    最近更新 更多