[英]How to bind a Grid in WPF when using MVC?
I know a way to do MVC binding of one string to one TextBox. 我知道一种方法来将一个字符串的MVC绑定到一个TextBox。 That's how it can be done: 这就是它的完成方式:
C#: C#:
namespace WpfApplication4
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = Model;
}
public ModelClass Model = new ModelClass();
private void button1_Click(object sender, RoutedEventArgs e)
{
Model.Output += "Setting New Output! ";
}
public class ModelClass : INotifyPropertyChanged
{
string _output;
public event PropertyChangedEventHandler PropertyChanged =
delegate { };
public string Output
{
get { return _output; }
set { _output = value;
PropertyChanged(this,
new PropertyChangedEventArgs("Output"));
}
}
}
}
}
XAML: XAML:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Button" VerticalAlignment="Top"
Name="button1" Click="button1_Click" />
<TextBox VerticalAlignment="Bottom"
Name="textBox1" Text="{Binding Path=Output}" />
</Grid>
</Window>
But I can't find a way to bind a two-dimensional array (or List) to a Grid or DataGrid. 但我找不到将二维数组(或List)绑定到Grid或DataGrid的方法。 Can you help me with it? 你能帮帮我吗? I couldn't find a working example on SO. 我在SO上找不到一个有效的例子。
Consider using a DataGrid to display your two-dimensional array, assuming you can store your data as a List<ColumnData>
where ColumnData is a class with one property per table column. 考虑使用DataGrid显示二维数组,假设您可以将数据存储为List<ColumnData>
,其中ColumnData是一个每个表列具有一个属性的类。
The WPF SDK contains a DataGrid, and there are several data grids from vendors available that have additional features. WPF SDK包含一个DataGrid,并且有几个供应商提供的数据网格具有其他功能。
if you wanna bind data to a datagrid you should be read something about the following. 如果您想将数据绑定到数据网格,您应该阅读以下内容。
ICollectionView, BindingListCollectionView ICollectionView,BindingListCollectionView
if you have somekind of collection you simply set the itemssource. 如果你有一些收藏品,你只需设置itemssource。
<DataGrid ItemsSource="{Binding Path=MyCollection, Mode=OneWay}" />
Collection types are mostly ObservableCollection or DataSet/DataTable. 集合类型主要是ObservableCollection或DataSet / DataTable。 if your collection supports editing and so on, you can do it with the datagrid. 如果您的集合支持编辑等,则可以使用datagrid进行编辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.