我有一个的ListBox 在我的用户控件中的一个,我想获得的的SelectedItem ,以在视图模型中使用。在的ListBox 是由的TextBlocks 的
这个问题是几乎直线上升回答我的问题,但我不明白的地方 DisneyCharacter (他的收藏型)来自何方,或者如何与的ListBox 。
请问我的是类型的的TextBlock ?
XAML为列表框的要求:
<列表框保证金=14,7,13,43 NAME =commandListBoxHEIGHT =470了minHeight =470了maxHeight =470WIDTH =248> < TextBlock的文本=设置输出高度=自动WIDTH =自动/> < TextBlock的文本=清除输出高度=自动WIDTH =自动/> < TextBlock的文本=按照左围边HEIGHT =自动WIDTH =自动/> < TextBlock的文本=按照右边围边HEIGHT =自动WIDTH =自动/> < TextBlock的文本=请带中心HEIGHT =自动WIDTH =自动/>< /列表框>解决方案
由于从TextBlock的输出是一个字符串, 。将绑定到一个字符串属性,你会绑定到您的视图模型或代码后面的字符串
< ListBox中的SelectedItem = {结合的myString}> ...... < /列表框>然后在无论你的DataContext设置像这样
字符串属性 公共字符串的myString {搞定;设置;}现在,每当你点击一个项目,从文字块文本将在myString的变量。
如果你使用MVVM模式你的财产是这样的:
私人字符串_myString; ///<总结> ///设置和获取myString的财产。 ///更改该属性的值提高PropertyChanged事件。 ///< /总结> 公共字符串myString的 {得到 {返回_myString; } 组 {如果(_myString ==值) {的回报; } RaisePropertyChanging(MyString的); _myString =价值; RaisePropertyChanged(MyString的); } }让我知道,如果你有任何问题。
I have a ListBox in one of my user controls in which I would like to get the SelectedItem, to be used in the ViewModel. The ListBox is composed of TextBlocks.
This question is pretty much a straight up answer to my question, but I don't understand where DisneyCharacter (his collection type) comes from, or how it relates to the ListBox.
Would mine be of type TextBlock?
XAML for ListBox as requested:
<ListBox Margin="14,7,13,43" Name="commandListBox" Height="470" MinHeight="470" MaxHeight="470" Width="248" > <TextBlock Text="Set Output" Height="Auto" Width="Auto" /> <TextBlock Text="Clear Output" Height="Auto" Width="Auto" /> <TextBlock Text="Follow Left Tape Edge" Height="Auto" Width="Auto" /> <TextBlock Text="Follow Right Tape Edge" Height="Auto" Width="Auto" /> <TextBlock Text="Follow Tape Center" Height="Auto" Width="Auto" /></ListBox>解决方案
Since the output from a TextBlock is a string, you would bind to a string property, you would bind to a string in your ViewModel or code behind.
<ListBox SelectedItem = "{Binding myString}"> ....... </ListBox>Then in whatever your datacontext is set up a string Property like this
public string myString {get; set;}Now whenever you click on an item, the text from that textblock will be in the myString variable.
if you were using MVVM model your property would look like this:
private string _myString; /// <summary> /// Sets and gets the myString property. /// Changes to that property's value raise the PropertyChanged event. /// </summary> public string myString { get { return _myString; } set { if (_myString == value) { return; } RaisePropertyChanging("myString"); _myString = value; RaisePropertyChanged("myString"); } }Let me know if you have any questions.
更多推荐
澄清绑定在MVVM中的Listbox.SelectedItem
发布评论