澄清绑定在MVVM中的Listbox.SelectedItem

编程入门 行业动态 更新时间:2024-10-20 11:25:27
本文介绍了澄清绑定在MVVM中的Listbox.SelectedItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个的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

本文发布于:2023-11-06 14:15:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563917.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   MVVM   SelectedItem   Listbox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!