In my WPF C# project I have two frames in the MainWindow. The first frame has a page with a DataGrid (bound to an XML file) in which I select an object of interest.

    <Grid.Resources>
        <XmlDataProvider x:Key="XmlData" Source="/DB.xml"/>
    </Grid.Resources>
    <DataGrid Name="dg"
              SelectionChanged="dg_SelectionChanged"
              AutoGenerateColumns="False"
              ItemsSource="{Binding Source={StaticResource XmlData}, XPath=Data/Object}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding XPath=Type}"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding XPath=Number}"></DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

In the second frame I open different pages (one at a time) according to the calculations I am going to perform with the selected object. At every SelectionChanged event a custom method MySub() is called, that initiates all the necessary calculations on the loaded page.

public partial class pg_DB : Page
{              
    public pg_DB()
    {
        InitializeComponent();
    }       

    public void dg_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        switch (Var._loadedPage) // This variable holds the name of the loaded page.
        {
            case "pg_SCT":
                pg_SCT c1 = new pg_SCT();
                c1.MySub();  // Initiates the calculation process on pg_SCT page. 
                break;
            case "pg_OCT":
                pg_OCT c2 = new pg_OCT();
                c2.MySub();  // Initiates the calculation process on pg_OCT page.
                break;
        }
    }
} 

The problem is that everything works well except the data visualization. Thus, for instance, every time the MySub() is called the List<> is being updated and the ItemsSource has the necessary items, yet they are not displayed in the DataGrid. Moreover, even simple TextBox1.Text = "Test" is not working. At the same time the same code works perfectly from the Button_Click method.

public partial class pg_SCT : Page       
{
    public pg_SCT()
    {
       InitializeComponent();
       //grid.ItemsSource = myList ();  (This works).    
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        //grid.ItemsSource = myList (); (This works).
        //TextBox1.Text = "Test";       (This works).
    }

    public void MySub()
    {
        grid.ItemsSource = myList(); // Nothing happens (although debugging shows that List is updated and ItemsSource has necessary items).
        TextBox1.Text = "Test";      // Textbox remains empty.    
    }

    public class Author
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

    private List<Author> myList()
    {
        List<Author> authors = new List<Author>();
        authors.Add(new Author()
        {
            ID = Var._ID,
            Name = Var._Name,
        });
        return authors;
    }                
}

I can’t find what is missing to populate DataGrid and TextBox from my custom method MySub().

Thank you for your time and consideration.

有帮助吗?

解决方案

In your dg_SelectionChanged method, you're creating instances of your Page controls as local variables but not using them anywhere so they are just going out of scope. I'm guessing you probably have other instances you've created elsewhere that are the ones being displayed but based on the code here the ones calling MySub are never going to show up.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top