XAML,将 Width 和 Height 属性绑定到其他控件的相同属性
我正在尝试创建反射效果,它工作得很好,只是我必须对一些值进行硬编码。这是我的 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}"
但它似乎不起作用。
这段代码有什么问题?
更新:如果我在这里设置宽度和高度:
<UserControl x:Name="CurrentPresenter" Height="200" Width="500" />
它会按预期工作。但是,如果我在 UserControl 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>
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过将
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
andWidth
properties of theBorder
to theActualHeight
andActualWidth
properties of the UserControl?将路径更改为 ViewportWidth!它对我有用
change path to ViewportWidth! its work for me