如何在两个 xaml 文件上创建数据绑定?
我试图对数据绑定的工作原理有一个有效的理解,但即使在学习了几个教程之后,我也只是对数据绑定的工作原理有了基本的了解。因此,对于那些更熟悉 silverlight 的人来说,这个问题似乎很重要。即使它很微不足道,请指出我一些处理这个问题的教程。我能找到的所有内容都只是通过在父 page.xaml 上添加数据绑定来解决这个问题(在我的情况下我不能使用它)。
为了这个例子,让我们假设我们有 5 个文件:
starter.cs
button1.xaml + codeBehind
button2.xaml + codeBehind
这两个按钮是在 starter(.cs) 文件中的代码中生成的,然后添加到某些 MapLayer
button1 my_button1 = new button1();
button2 my_button1 = new button2();
someLayer.Children.Add(my_button1);
someLayer.Children.Add(my_button2);
我的目标是连接这两个按钮,以便它们始终显示相同的“文本”(即 my_button1.content==my_button2.content = true;)。因此,当 my_button1.content 发生更改时,此更改应该传播到另一个按钮(双向绑定)。
目前我的 Button1.xaml 看起来像这样:
<Grid x:Name="LayoutRoot">
<Button x:Name="x_button1" Margin="0,0,0,0" Content="{Binding ElementName=x_button2, Path=Content}" ClickMode="Press" Click="button1_Click"/>
</Grid>
但我从中得到的一切都是一个根本没有内容的按钮,它只是空白,因为绑定默默地失败了。 如何在我描述的上下文中创建数据绑定?最好是代码而不是 XAML ;)
提前致谢
I am trying to come to a working understanding of how databinding works, but even after several tutorials I only have a basic understanding of how databinding works. Thus this question might seem fundamental to those more familiar with silverlight. Even if it is trivial, please point me to some tutorial that deals with this problem. All that I could find simply solved this via adding the data binding on a parent page.xaml (that i must not use in my case).
For the sake of this example let us assume, that we have 5 files:
starter.cs
button1.xaml + codeBehind
button2.xaml + codeBehind
The two buttons are generated in code in the starter(.cs) file, and then added to some MapLayer
button1 my_button1 = new button1();
button2 my_button1 = new button2();
someLayer.Children.Add(my_button1);
someLayer.Children.Add(my_button2);
My aim is to connect the two buttons, so that they always display the same "text" (i.e. my_button1.content==my_button2.content = true;). Thus when something changes my_button1.content this change should be propagated to the other button (two way binding).
At the moment my button1.xaml looks like this:
<Grid x:Name="LayoutRoot">
<Button x:Name="x_button1" Margin="0,0,0,0" Content="{Binding ElementName=x_button2, Path=Content}" ClickMode="Press" Click="button1_Click"/>
</Grid>
But everthing that i get out of that is a button with no content at all, it is just blank as the binding silently fails.
How could I create the databinding in the context I described? Preferably in code and not XAML ;)
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要阅读的文档块是这样的: XAML 名称范围
您的button1 xaml 有一个绑定,用于查找名为“x_button2”的元素。然而,在实际应用程序中可能有许多控件,而这些控件又具有嵌套控件。所有这些控件都具有各种 UI 元素,其中一些元素可能有名称。
如果整个应用程序中的所有名称都是唯一的,那么就不可能完成任何事情。然而,如果您的 Button1 想要能够在可视化树中的某个位置找到它实际知道的控件(它自己的 xaml)之外的另一个控件的存在,那么这就需要是正确的。
因此,每个加载的 Xaml 文档都存在于其自己的“名称范围”中,并且对具有其他名称的其他元素的搜索仅限于该“名称范围”。
这个问题有多种解决方案,具体取决于您的实际需求,而不是您问题中的简化问题。
通常,您为每个控件提供一个
DependencyProperty
,内部按钮Content
属性将绑定到该控件。在“MapLayer”中,可以将其中一个按钮控件的属性绑定到另一个按钮控件。The chunk of documentation you need to read is this: XAML Namescopes
Your button1 xaml has a binding looking for an element with the name "x_button2". However in a real application there can be many controls which in turn have nested controls. All of these controls have all manner of UI elements some of which may have names.
It would be impossible to get anything done if all names throughout the entire application had be unique. Yet that would need to be true if it were for your button1 to be able to hunt down the existence of another control somewhere in the visual tree outside of that which it actually knows (its own xaml).
Hence each loaded Xaml document exists in its own "namescope" and the search for other elements with other names is limited to that "namescope".
The are various solutions to this problem depending on what you real requirements are as opposed to the simplified problem in your question.
Typically you give each of your controls a
DependencyProperty
to which the inner buttonContent
property binds. In "MapLayer" as call it, could then bind the propert on one of your button controls to the other.