简体   繁体   English

后面的代码中的xaml绑定等效于什么?

[英]What is the equivalent of this xaml binding in code behind?

I have a xaml TextBlock bound as follows: 我有一个xaml TextBlock绑定,如下所示:

 <TextBlock Text="{Binding LastName}"/>

How do I access SelectedItem.LastName in C# codebehind? 如何在C#代码背后访问SelectedItem.LastName? Thanks. 谢谢。

Details: 细节:

Data are from an XML file 数据来自XML文件

 <Player>
 <LastName>...</LastName>
 <Age>...</Age>
 </Player>

The combobox binding was simplified for clarity as above in the xaml file. 为简化起见,在xaml文件中如上所述简化了组合框绑定。

Looking for this? 寻找这个吗? http://msdn.microsoft.com/en-us/library/ms742863.aspx http://msdn.microsoft.com/en-us/library/ms742863.aspx

MyData myDataObject = new MyData(DateTime.Now);      
Binding myBinding = new Binding("MyDataProperty");
myBinding.Source = myDataObject;
myText.SetBinding(TextBlock.TextProperty, myBinding);

SelectedItem belongs to `playerComboBox'. SelectedItem属于`playerComboBox'。 So it should be: 因此应该是:

(playerComboBox.SelectedItem as TypeOfselectedItem).LastName

Modified answer above: 上面的修改答案:

var selectedPlayer = (Player)playerComboBox.SelectedItem;
var age = selectedPlayer.Age;
var lastName = selectedPlayer.LastName;

Seems like this is what you're looking for. 看起来这就是您想要的。


UPDATE (if "there is no Player class" ): 更新 (如果“没有播放器类” ):

dynamic player = playerComboBox.SelectedItem;
int age = player.Age;
string lasName = player.LastName;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM