Click here to Skip to main content
15,508,758 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I am working on Model View Architecture scheme. I have defined string Array that contains data from three different sources in ViewModel.
I need to bind 3 text boxes in View (User Interface) from different array elements that comes from ViewModel.
I am using WPF application that contains XAML

Thanks

What I have tried:

I have bind the text property of text boxes to those array elements. But it is not working. How can I do this.
Posted
Updated 31-Mar-17 10:58am
Comments
Graeme_Grant 30-Mar-17 12:28pm    
see this post for a similar question and answer: How do I get the state of radio buttons inside a list to data-bind?[^]

You do not describe exactly what is not working. Binding to a String array property MyArray like this should work:
<TextBox Text="{Binding MyArray[0]}" /><Label Content="{Binding MyArray[0]}" />
<TextBox Text="{Binding MyArray[1]}" />
<TextBox Text="{Binding MyArray[2]}" />

Change notification will however not work, so updating the first element by entering something in the first text box will store the value in the array, but not lead to that Label bound the same element is updated.
 
Share this answer
 
Comments
Wahaj Khan 31-Mar-17 2:06am    
The scheme you describe is not working..Infact I need to populate three text boxes from data coming from different locations, say 3 voltages values of same type from 3 different hard wares.
One way for this is to define 3 public properties each holding values and bind them to 3 text boxes as shown below:
<TextBox Name = "Text1" Text="{Binding TextValue1}" />
<TextBox Name = "Text2" Text="{Binding TextValue2}" />
<TextBox Name = "Text3" Text="{Binding TextValue3}" />

This scheme works fine and has been doing this for many months and populates data in correct manner.
However the type of data now coming from hard ware is numerous say 40 voltages. so we can't define 40 * 3 = 120 properties rather we need to define public Arrays for each kind of voltage and bind Array elements to individual text boxes as you suggested.
But this is not working. Rather it says
"Path Item MyArray[0] could not be resolved."
When we select Apply Data binding for Text property.
Hi again,

if you have like 40x3 items it sounds that you should use a DataGrid. If you have the values in a String[][] MyMatrix property then you could bind to this "jagged" array like this:
<DataGrid ItemsSource="{Binding MyMatrix}" AutoGenerateColumns="False" >
   <DataGrid.Columns>
       <DataGridTextColumn Binding="{Binding .[0]}" />
       <DataGridTextColumn Binding="{Binding .[1]}" />
       <DataGridTextColumn Binding="{Binding .[2]}" />
   </DataGrid.Columns>
</DataGrid>

The type of MyMatrix could also be ObservableCollection<String[]>
 
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