本文介绍了如何绑定用户控件以便共享属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 这是我的第一篇文章,所以如果我没有正确解释或提出我的问题,我会提前道歉。我正在处理一个应用程序,其中包含一个具有多个用户控件的类,我将其用作Tab项。每个用户控件都包含其他控件,如按钮和文本框等。下面的代码片段: Window x:Class =TeamShootout.TeamShootoutMainWindow xmlns =http://schemas.microsoft .com / winfx / 2006 / xaml / presentation xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml xmlns:Tournaments =clr-namespace:TeamShootout .Tournaments xmlns:RoundMatchup =clr-namespace:TeamShootout.TeamRoundMatchup xmlns:TeamScoring =clr-namespace:TeamShootout.TeamScoring xmlns:BestBallScoring =clr-namespace: TeamShootout.BestBallScoring xmlns:BestBallRoundMatchup =clr-namespace:TeamShootout.BestBallRoundMatchup < TabControl > < TabItem 标题 = 锦标赛 > < 锦标赛:TournamentsUserControl / > < / TabItem > <! - Round Roundup选项卡的控件。 - > < TabItem 标题 = 团队匹配 > < RoundMatchup:TeamRoundMatchupUserControl / > < / TabItem > < TabItem 标题 = 团队评分 > < TeamScoring:TeamScoringUserControl / > < / TabItem > < TabItem 标题 = 最佳球赛 > < BestBallRoundMatchup:BestBallRoundMatchupUserControl / > < / TabItem > < TabItem 标题 = 最佳球得分 > < BestBallScoring:BestBallScoringUserControl / > < / TabItem > < / TabControl > 我想要完成的是当用户在其中一个选项卡上选择某个值时,该值被绑定到另一个选项卡项/用户控制,因此当用户转到该选项卡时,关联的用户控件知道选择了什么。例如,如果用户从锦标赛选项卡项目中选择锦标赛,我希望该锦标赛号码可用于团队比赛选项卡项目。我在用户控件中成功绑定,但在用户控件之间没有成功。建议?解决方案 听起来我想把你的数据存储在所有标签绑定到的集合中。这样,它就是一个包含数据的数据存储,每个选项卡都来自一个公共数据源。 您需要一个公开数据集和所选项的数据模型类。它应该实现INotifyPropertyChanged接口,如果你要在多个页面或视图中使用它,它应该是静态的或实现Singleton模式。 该类应该暴露你的数据集合作为属性并将单个项目作为选定属性。选定的属性应确保在设置属性时引发PropertyChanged事件。绑定到该属性时,您的UI控件将响应该事件。 然后,您需要将数据模型类设置为控件/页面的数据上下文。您可以通过设置this.DataContext = new DataModelClass在后面的代码中执行此操作;或者通过在xaml中声明数据上下文并将其设置为数据模型类的实例。 完成后,可以将属性绑定到属性在数据模型类上。例如,对于组合框,xaml可以是:< combobox selecteditem = {绑定SelectedItem,Mode = TwoWay} /> 它告诉绑定引擎将组合框上的选定项属性连接到数据模型类的属性并使关系成为两个办法。如果在一个选项卡上执行此操作,则在另一个选项卡上绑定文本框或其他控件(如果只需要读取权限,则跳过模式语句),然后当组合框中的选定项更改时,另一个选项卡上的文本框文本选项卡将更改。 绑定是xaml的基础。我建议你阅读数据绑定,特别是MVVM的一些内容,以帮助我们更好地理解。你要问的是非常简单的,并且可以通过很多方式完成......我已经提出了一个......但是你需要了解每种方法,因为它可以在易于开发/维护与性能和可扩展性方面进行权衡。 br /> 你可能想查看Tim Heuer在Silverlight上关于数据绑定的帖子 http://timheuer.com/blog/articles/getting-started-with-silverlight-development.aspx [ ^ ]虽然适用于Silverlight,但原则是相同的。 This is my first post, so if I don''t explain or ask my questions correctly I apologize in advance. I am working on an application that contains a class with multiple user controls that I am using as Tab Items. Each of the User Controls contains other controls like buttons and text boxes and the like. Code snippet below:Window x:Class="TeamShootout.TeamShootoutMainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Tournaments="clr-namespace:TeamShootout.Tournaments" xmlns:RoundMatchup="clr-namespace:TeamShootout.TeamRoundMatchup" xmlns:TeamScoring="clr-namespace:TeamShootout.TeamScoring" xmlns:BestBallScoring="clr-namespace:TeamShootout.BestBallScoring" xmlns:BestBallRoundMatchup="clr-namespace:TeamShootout.BestBallRoundMatchup" <TabControl> <TabItem Header="Tournaments"> <Tournaments:TournamentsUserControl /> </TabItem> <!--Controls for the Round Matchup tab.--> <TabItem Header="Team Matchup"> <RoundMatchup:TeamRoundMatchupUserControl /> </TabItem> <TabItem Header="Team Scoring"> <TeamScoring:TeamScoringUserControl /> </TabItem> <TabItem Header="Best Ball Matchup"> <BestBallRoundMatchup:BestBallRoundMatchupUserControl /> </TabItem> <TabItem Header="Best Ball Scoring"> <BestBallScoring:BestBallScoringUserControl /> </TabItem> </TabControl>What I am trying to accomplish is when the user selects some value on one of the tabs that that value is binded to another Tab Item/User Control, so when the user goes to that tab the associated User Control knows what was selected. For example, if the user selects a tournament from the Tournaments Tab Item, I want that tournament number to be available to the Team Matchup Tab Item. I am successful in binding within a User Control but not between User Controls. Suggestions? 解决方案 It sounds to me like you want to store your data in a collection that all your tabs bind to. That way, it''s one data store that contains your data, and each tab renders from a common data source.You need a data model class that exposes your data collections and selected items. It should implement the INotifyPropertyChanged interface and it should be static or implement the Singleton pattern if you will use it across multiple pages or views.That class should expose your data collections as a property and hold a single item as the "selected" property. The selected property should make sure that it raises the PropertyChanged event when the property is set. Your UI controls will respond to that event when bound to that property.You then need to set the data model class as your data context for the controls/page. You can either do this in the code behind by setting this.DataContext = new DataModelClass; or by declaring the data context in xaml and setting it to an instance of you data model class.Once you have done that, you can bind the properties to the properties on the data model class. For example, for a combobox the xaml may be: <combobox selecteditem="{Binding SelectedItem, Mode=TwoWay}" /> That tells the binding engine to wire up the selected item property on the combobox to the property on your data model class and make the relationship two way. If you do this on one tab then bind a text box or some other control on another tab (skipping the mode statement if it only needs read access) then when the selected item in the combo box is changed, the text box text on the other tab will change.Bindings are fundamental to xaml. I would suggest you read-up on data bindings, especially some of the stuff around MVVM, to help get a better understanding. What you are asking is pretty straightforward and can be done lots of way... I''ve suggested one... But you need to understand each approach as it can have tradeoff in ease of development/maintenance versus performance and scalability.You might want to check out Tim Heuer''s post on data binding in Silverlight at http://timheuer.com/blog/articles/getting-started-with-silverlight-development.aspx[^] While it is for Silverlight, the principles are the same. 这篇关于如何绑定用户控件以便共享属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 21:55