C#如何使用WPF绑定来自不同数组元素的多个文本框 [英] C# how to bind multiple text boxes from different array elements using WPF

查看:236
本文介绍了C#如何使用WPF绑定来自不同数组元素的多个文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi
我正在研究模型视图架构方案。我已经定义了包含来自ViewModel中三个不同源的数据的字符串数组。

我需要在View(用户界面)中绑定来自ViewModel的不同数组元素的3个文本框。

我正在使用包含XAML的WPF应用程序



谢谢



< b>我尝试了什么:



我已经将文本框的text属性绑定到那些数组元素。但它没有用。我怎么能这样做。

解决方案

你没有准确描述什么是无效的。绑定到String数组属性这样的MyArray应该可以工作:

< TextBox Text ={Binding MyArray [0]}/>< Label Content ={绑定MyArray [0]}/> 
< TextBox Text ={Binding MyArray [1]}/>
< TextBox Text ={Binding MyArray [2]}/>



更改通知无效,因此请输入更新第一个元素第一个文本框中的某些内容会将值存储在数组中,但不会导致Label绑定相同的元素更新。


嗨再次,



如果你有40x3项,听起来你应该使用DataGrid。如果你有 String [] [] MyMatrix 属性中的值,那么你可以像这样绑定到这个锯齿状数组:

< DataGrid ItemsSource ={Binding MyMatrix}AutoGenerateColumns =False> 
< DataGrid.Columns>
< DataGridTextColumn Binding ={Binding。[0]}/>
< DataGridTextColumn Binding ={Binding。[1]}/>
< DataGridTextColumn Binding ={Binding。[2]}/>
< /DataGrid.Columns>
< / DataGrid>



MyMatrix的类型也可以是 ObservableCollection< String []>


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.

解决方案

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.


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[]>


这篇关于C#如何使用WPF绑定来自不同数组元素的多个文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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