XAML, binding Width and Height properties to the same properties of other control

时间秒杀一切 提交于 2019-12-09 16:55:33

问题


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>

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.

Wha's wrong with this code?

UPDATE: If I set Width and Height here:

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

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


回答1:


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}" >



回答2:


change path to ViewportWidth! its work for me



来源:https://stackoverflow.com/questions/2153316/xaml-binding-width-and-height-properties-to-the-same-properties-of-other-contro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!