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
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. 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
<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");
}
}
TblData2is defined. It has to be property and it has to callPropertyChangedin setter. - Sinatr