简体   繁体   中英

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

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");
        }
    }

Try to enter the following code in your ViewModel.

1.Add an ObservableCollection property with all selected texts

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

public void AddSelectedText(string selectedText)
{

     SelectedTexts.Add(selectedText);


}

3.xaml data binding

<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. Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM