XAML,将Width和Height属性绑定到其他控件的相同属性 [英] XAML, binding Width and Height properties to the same properties of other control
问题描述
我正在尝试创建一种反射效果,并且除了必须对一些值进行硬编码外,它的工作效果非常好.这是我的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?
推荐答案
您是否尝试将Border
的Height
和Width
属性绑定到用户控件,而不是绑定到UserControl的Height和Width属性. UserControl的ActualHeight
和ActualWidth
属性?
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屋!