Click here to Skip to main content
15,894,250 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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;
        }


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
Posted
Comments
BobJanova 3-Jun-14 6:15am    
Can you just cast it?

ValueBoxControl control = (ValueBoxControl)List.SelectedItem;

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.
Member 10151063 3-Jun-14 7:13am    
I love you!

It works perfectly. Well, I'm pretty new to C# and programming alltogether
(1 Year), so I don't know many of the tricks. Anyways, thank you very much. ^-^
BobJanova 3-Jun-14 14:16pm    
Cool. I'll post it as an answer then since otherwise the question still looks unanswered.

Ed: check out some articles about WPF data binding, though. You'll really want to learn that stuff if you're staying in WPF.

1 solution

Cast the item out of the list:

ValueBoxControl control = (ValueBoxControl)List.SelectedItem;


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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900