[英]Bind DataTable to Datagrid in WPF MVVM in runtime
I'm developing a WPF application with MVVM design pattern, in my first Window i want to display a datagrid that is created with the selected text of a text box This is a preview of what i want to do 我正在开发具有MVVM设计模式的WPF应用程序,在我的第一个窗口中,我想显示一个使用文本框的选定文本创建的数据网格。 这是我要执行的操作的预览
In my ViewModel I implemented a method that fill the datatable with the selectedText and then bind it to DataGrid, but My DataGrid don't show anything. 在我的ViewModel中,我实现了一种方法,该方法用selectedText填充数据表,然后将其绑定到DataGrid,但是My DataGrid不显示任何内容。 This is my method 这是我的方法
void selectColumn(object parameter)
{
string selText = SelectedText;
if (i == 0)
{
var lines = File.ReadAllLines(TextProperty1);
datatable.Columns.Add("Column" + i + "");
foreach (string line in lines)
{
DataRow newRow = datatable.NewRow();
newRow["Column" + i + ""] = line.Substring(0, selText.Length);
datatable.Rows.Add(newRow)
}
i++;
}
else
{
datatable.Columns.Add("Column" + i + "");
var lines = File.ReadAllLines(TextProperty1);
foreach (DataRow draw in datatable.Rows)
{
draw["Column" + i + ""] = lines[datatable.Rows.IndexOf(draw)].Substring(lines[2].IndexOf(selText), selText.Length);
}
TblData2 = datatable;
i++;
}
TblData2 = datatable;
TextProperty2 = TextProperty2.Remove(0, selText.Length);
}
and in the Window this is how i am binding Datagrid 在窗口中,这就是我绑定Datagrid的方式
<TextBox x:Name="txt" Text="{Binding TextProperty2, UpdateSourceTrigger=PropertyChanged}">
<i:Interaction.Behaviors>
<i:DependencyPropertyBehavior PropertyName="SelectedText" EventName="SelectionChanged" Binding="{Binding SelectedText, Mode=TwoWay}"/>
</i:Interaction.Behaviors>
</TextBox>
<Button x:Name="Tex" Content="Select Column" Command="{Binding SelectedColumnCommand}"/>
<DataGrid x:Name="DtGrid" ItemsSource="{Binding TblData2}"/>
This is the datatable 这是数据表
DataTable _dataTable2;
public DataTable TblData2
{
get { return _dataTable2; }
set
{
_dataTable2 = value;
RaisePropertyChanged("TblData");
}
}
Try to enter the following code in your ViewModel. 尝试在ViewModel中输入以下代码。
1.Add an ObservableCollection property with all selected texts 1.添加带有所有选定文本的ObservableCollection属性
ObservableCollection<string> _SelectedTexts;
public ObservableCollection<string> SelectedTexts
{
get { return _SelectedTexts; }
set
{
_SelectedTexts; = value;
RaisePropertyChanged("SelectedTexts");
}
}
public YourViewModelConstructor
{
SelectedTexts = new ObservableCollection<string>();
}
2.Add selected text in ObservableCollection 2.在ObservableCollection中添加选定的文本
public void AddSelectedText(string selectedText)
{
SelectedTexts.Add(selectedText);
}
3.xaml data binding 3.xaml数据绑定
<DataGrid x:Name="DtGrid" ItemsSource="{Binding SelectedTexts}"/>
I didn't check all of the code but binding the ItemsSource to some property and then change that property at runtime will not work, you will have to use an ObservableCollection. 我没有检查所有代码,但将ItemsSource绑定到某个属性,然后在运行时更改该属性将无法工作,您将必须使用ObservableCollection。 Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.