通过 ElementName 将 UserControl 属性绑定到其子属性会导致绑定错误
我有以下 XAML:
<UserControl x:Class="WpfWindow.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.RenderTransform>
<TranslateTransform X="{Binding ElementName=MySlider, Path=ActualWidth}" />
</UserControl.RenderTransform>
<Grid>
<Slider x:Name="MySlider" Canvas.Left="41" Canvas.Top="86" Height="23" Width="100" Minimum="0" Maximum="100"/>
</Grid>
</UserControl>
当我尝试使用内部包含 UserControl 的窗口时,我得到:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=MySlider'. BindingExpression:Path=ActualWidth; DataItem=null; target element is 'TranslateTransform' (HashCode=53368240); target property is 'X' (type 'Double')
这特别奇怪,因为直接在 Window 中使用相同的代码可以完美地工作。
目前,我已经通过在代码中设置绑定解决了该问题,但是,我不知道为什么我的版本不起作用,如果可能的话,我宁愿将所有内容都放在 XAML 中。
谢谢!
I have the following XAML:
<UserControl x:Class="WpfWindow.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.RenderTransform>
<TranslateTransform X="{Binding ElementName=MySlider, Path=ActualWidth}" />
</UserControl.RenderTransform>
<Grid>
<Slider x:Name="MySlider" Canvas.Left="41" Canvas.Top="86" Height="23" Width="100" Minimum="0" Maximum="100"/>
</Grid>
</UserControl>
When I try to use a window with the UserControl inside I get:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=MySlider'. BindingExpression:Path=ActualWidth; DataItem=null; target element is 'TranslateTransform' (HashCode=53368240); target property is 'X' (type 'Double')
It is especially strange since using the same code directly in a Window works flawlessly.
For now I've resolved the issue by setting the binding in code, however, I have no idea why my version doesn't work and I'd rather have everything in XAML if possible.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我注意到有时在
Window
/UserControl
等上使用 ElementName 设置绑定时,声明的顺序很重要。我不确定这样做的原因,但如果您在设置
之前声明Grid
我认为它会起作用I've noticed that sometimes when setting Bindings with ElementName on
Window
/UserControl
etc. the order of declaration is important. I'm not sure of the reason for this but if you declare theGrid
before you set<UserControl.RenderTransform>
I think it'll work我实际上有类似的问题,想分享我的解决方案。
我有一组着色器,我想将其应用于边框。
着色器的属性绑定到控件的值(滑块值)。
XAML:
...
在 Window 中工作得很好,但在我将该 XAML 移至用户控件后,它就停止工作了。
您无法将 Window.Resources 移动到用户控件的底部。
因此,对我有用的解决方案是在容器中分配资源(在我的例子中为边界),而不是从资源部分引用它们:
我相信这是一个错误,但至少有解决方法。
I actually have sort of similar issue and would like to share my solution.
I have set of shaders, which I want to apply to the borders.
Shader's properties binded to the control's values (slider value).
XAML:
...
Works perfectly while in Window, but after I moved that XAML to the User Control it just stop working.
You cannot move Window.Resources to the bottom of the user control.
So, solution, which works for me was to assign resources right in the container (Border in my case) instead of referencing them from the Resource section:
I believe this is a bug, but at least there is workaround.