如何从ListView中的项目中获取Control本身? [英] How do I receive a Control itself out of an Item in a ListView?

查看:100
本文介绍了如何从ListView中的项目中获取Control本身?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我遇到了一个看似无法解决的问题。



我有创建了两个不同的UserControl。



第一个有一个TextBlock和一个TextBox

(名称:ValueBoxControl)



第二个与第一个完全一样,除了我已经添加了一个ListView。

(名称:ListViewControl)



现在我已将ValueBoxControl 6x添加到ListViewControl(进入ListView)。

这都是代码隐藏。我没有使用XAML。



好​​的,到目前为止ListViewControl的ListView中有6个项目。这些项都是ValueBoxControl。



在Item获得Focus之后,我想用ListViewControl的TextBox / TextBlock切换ValueBoxControl TextBox / TextBlock的值。





为了达到这个目的,我必须阅读ValueBoxControl的内容,INSIDE当前所选的ListView项目。

所以,我的问题是:我该怎么做?有没有像List.SelectedItem.Content.Text?当然不是,但我该怎么做?我绝对绝望,整个万维网都没有单一的解决方案。



Hello everyone!

I've run across a seemingly unsolvable problem.

I have created two different UserControls.

The first one has one TextBlock and one TextBox
(name: ValueBoxControl)

The second one is exactly like the first one, except that I've added a ListView to it.
(name: ListViewControl)

Now I have added the ValueBoxControl 6x to the ListViewControl (into the ListView).
This is all code-behind. I don't use XAML for this.

Okay, so far there are 6 Items in my ListView of the ListViewControl. These Items are all ValueBoxControls.

After an Item gets the Focus, I want to switch the values of the ValueBoxControl TextBox / TextBlock with the TextBox / TextBlock of the ListViewControl.


To achieve that I have to read the content of the ValueBoxControl, INSIDE the currently selected Item of the ListView.
So, my question is: How do I do that? Is there something like a List.SelectedItem.Content.Text? Of course not, but how do I do that? I am absolutely desperate and there's not a single solution on the whole World Wide Web.

private void SelectItem()
        {
            string clipContent;
            string clipTitle;
            string itemContent;
            string itemTitle;
            ValueBoxControl control = List.SelectedItem;

            //TextBox and TextBlock of ListViewControl
            clipContent = ValueTextBox.Text;
            clipTitle = TitleText.Text;        

            //TextBox and TextBlock of ValueBoxControl
            itemContent = control.Text;
            itemTitle = control.TitleText.Text;

            control.Text = clipContent;
            control.TitleText.Text = clipTitle;

            //Text and TitleText.Text are the TextBox and TextBlock of the 
            //ListViewControl.
            Text = itemContent;
            TitleText.Text = itemTitle;
        }





这段代码显然无效,但这与我的想法有关。问题是List.SelectItem返回一个对象类型,而ValueBoxControl是一个UserControl。



问候

Narakuvera



This code obviously won't work, but that's about the idea I have. The problem is that List.SelectItem returns an object type, while ValueBoxControl is a UserControl.

Greetings
Narakuvera

推荐答案

将项目从列表中删除:



Cast the item out of the list:

ValueBoxControl control = (ValueBoxControl)List.SelectedItem;





实际上你应该将两个控件的数据绑定到同一个视图模型层,所以当你更改列表视图中的选定项目时它导致另一个控件通过数据源而不是通过这种UI意大利面更新。



Really though you should be data binding both controls to the same view model layer, so when you change selected item on the list view it causes the other control to update through the data source and not through this kind of UI spaghetti.


这篇关于如何从ListView中的项目中获取Control本身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆