XAML,将Width和Height属性绑定到其他控件的相同属性 [英] XAML, binding Width and Height properties to the same properties of other control

查看:84
本文介绍了XAML,将Width和Height属性绑定到其他控件的相同属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一种反射效果,并且除了必须对一些值进行硬编码外,它的工作效果非常好.这是我的XAML:

I'm trying to create a reflection effect and it's working great except that I have to hard-code some values. This is my 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>

我试图用Width="{Binding ElementName=CurrentPresenter, Path=Width}"Height="{Binding ElementName=CurrentPresenter, Path=Height}"替换Border的Width="500"Height="200",但似乎不起作用.

I've tried to replace Border's Width="500" and Height="200" by Width="{Binding ElementName=CurrentPresenter, Path=Width}" and Height="{Binding ElementName=CurrentPresenter, Path=Height}" but it doesn't seem to work.

此代码有什么问题?

更新:如果我在此处设置宽度和高度:

UPDATE: If I set Width and Height here:

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

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

It works as expected. However it doesn't work if I set those values in the UserControl XAML. Any ideas?

推荐答案

您是否尝试将BorderHeightWidth属性绑定到用户控件,而不是绑定到UserControl的Height和Width属性. UserControl的ActualHeightActualWidth属性?

Rather than bind to the Height and Width properties of the UserControl, have you tried binding the Height and Width properties of the Border to the ActualHeight and ActualWidth properties of the UserControl?

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

这篇关于XAML,将Width和Height属性绑定到其他控件的相同属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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