0

I am trying to solve problem with communication between items in listbox with template. Lets say i have template:

<DataTemplate x:Key="Template">
    <StackPanel HorizontalAlignment="Stretch">
        <TextBox Name="A" FontSize="17" Margin="5,5,5,1"></TextBox>
        <TextBox Name="B" FontSize="15" Margin="5,0,5,1"></TextBox>
    </StackPanel>
</DataTemplate>

And when i type data on Textbox B and press ENTER i call method that sends typed text to Textbox A. Problem is how i can create such keydown metod for template items in listbox item and what if i will heve more than one item?

Justin XL
  • 38,763
  • 7
  • 88
  • 133
Grzesiu
  • 9
  • 1
  • 5

2 Answers2

0

Why don't you try binding like this

<DataTemplate x:Key="Template">
    <StackPanel HorizontalAlignment="Stretch">
        <TextBox Name="A" Text="{Binding Text, ElementName=B}" FontSize="17" Margin="5,5,5,1" />
        <TextBox Name="B" FontSize="15" Margin="5,0,5,1" />
    </StackPanel>
</DataTemplate>
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
  • It have to be like chat message. When i type in B and send it, B is cleared out of all text. – Grzesiu Nov 09 '13 at 13:13
  • You can add event in textboxes. – Nikhil Agrawal Nov 09 '13 at 13:15
  • I add the event on keydown to my textbox B. Its ok i will send message and clear the window but what about sending the data to textbox A ? I cant call it becouse it dont exist in context. I cant call it like B: ((TextBox)sender).Text. Any ideas ? – Grzesiu Nov 09 '13 at 13:42
  • When b event is raised, use Parent property to get Stackpanel. After that use FindVisualChildren method to get A textbox.(google for FindVisualChildren). – Nikhil Agrawal Nov 09 '13 at 13:45
  • Well I cant pass through it. Its windows 8 store app and I think some methods and objects are different from these in WPF. I did the 'VisualTreeHelper'. I can find my 'TextBox'A with method 'GetChild(parent, index)' But now I think when I change my template everything will be broken. Also lets say i want to get to the specific `TextBox` A in the specific `Item` I have no idea how to get to it. I mean i have `IncomingMessage` method where i need to find the item that is my contact and place his message to his `TextBox` A. – Grzesiu Nov 09 '13 at 15:17
0

Lets say I got it. But its kind of problematic one. Its windows 8 store app and I think I cant process with that problem like in WPF. Thats how i did it:

                    var parent = ((TextBox)sender).Parent;
                var child = VisualTreeHelper.GetChild(parent, 1);
                TextBox text = (TextBox)child;
                text.Text += Client.Settings.Account + ": " + ((TextBox)sender).Text + "\n";
                ((TextBox)sender).Text = string.Empty;

And that one problem made another two:

  • When I change my template everything is going to be broken.
  • Lets say i want to get to the specific TextBox A in the specific Item I have no idea how to get to it. I mean i have IncomingMessage method where i need to find the item that is my contact and place his message to his TextBox A.
Grzesiu
  • 9
  • 1
  • 5
  • Found solution here: [link](http://stackoverflow.com/questions/5181063/how-to-access-a-specific-item-in-a-listbox-with-datatemplate) I made a helper class with find descendant method that lets me to get my control. Thanks all for help. – Grzesiu Nov 10 '13 at 13:07