Click here to Skip to main content
14,450,136 members
Rate this:
Please Sign up or sign in to vote.
See more:

I have a DataGrid in my MainWindow.xaml which has various columns. I have another windows 'AddFixtures.xaml' which has a load of text boxes in, I want the data from the text boxes in this window to go to rows in the datagrid in MainWindow.xaml.

What I have tried:

I have a class for Fixture in the code behind 'AddFixture.xaml:

public class Fixture
            public int fixtureID { get; set; }
            public int channelID { get; set; }
            public string fixtureName { get; set; }
            public string position { get; set; }
            public string patch { get; set; }
            public string mode { get; set; }
            public string power { get; set; }
            public string direction { get; set; }

which then I get all the data from the various text boxes when I click the 'Ok' button:

private void BtnAddFixture_Click(object sender, RoutedEventArgs e)
            var data = new Fixture {
                fixtureID = Int32.Parse(txtFixtureID.Text),
                channelID = Int32.Parse(txtChannel.Text),
                fixtureName = cbxFixture.Text,
                position = cbxPosition.Text,
                patch = cbxUniverse.Text + "." + txtChannel.Text,
                mode = cbxMode.Text,
                power = cbxSocca.Text + "." + cbxWay.Text,
                direction = "Forwards"


So I literally just need to get the var 'data' to my 'MainWindow' to add the rows.

I have binded the class to my datagrid columns like this:

<DataGrid x:Name="FixtureView"  AutoGenerateColumns="True" Grid.Column="1">
                    <DataGridTextColumn Binding="{Binding fixtureID}" Header="Fixture ID" Width="*"/>
                    <DataGridTextColumn Binding="{Binding channelID}" Header="Channel ID" Width="*"/>
                    <DataGridTextColumn Binding="{Binding fixtureName}" Header="Name" Width="3.5*"/>
                    <DataGridTextColumn Binding="{Binding position}" Header="Position" Width="2*"/>
                    <DataGridTextColumn Binding="{Binding patch}" Header="Patch" Width="*"/>
                    <DataGridTextColumn Binding="{Binding mode}" Header="Mode" Width="1.5*"/>
                    <DataGridTextColumn Binding="{Binding power}" Header="Power" Width="*"/>
                    <DataGridTextColumn Binding="{Binding direction}" Header="Orientation" Width="2*"/>

Would be very greatfull if anyone could tell me what I need to do, have been searching for hours!
Updated 7-Nov-19 3:52am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You might be looking for ObservableCollection, see example here: WPF Update Datagrid Binding To ObservableCollection[^]

Also see answers here: WPF passing data between windows[^]

Also see: Using the DataContext - The complete WPF tutorial[^]

Here is another example: c# - Binding a property from a class to XAML directly - Stack Overflow[^]
Note the use of the namespace:
<Window xmlns:da="clr-namespace:WPFTestBinding.DataAccess">
Member 14647867 7-Nov-19 8:57am
Bit confused as the answer there doesn't really explain what I need to add. I just need a way of passing the data through to my Main Window. I've done this before I think with a DataSet but I can't remember how
Maciej Los 7-Nov-19 9:09am
If you would like to display a collection of fixtures in main window, you have to create a list of fixtures and then create a view for that collection. In 'AddFixtures' window you have to provide an ability to add a fixture to the collection of fixtures. That's all.
Member 14647867 7-Nov-19 9:15am
I have a button in 'AddFixtures' that gets all the data from textboxes and assigns it as a Fixture class.
Maciej Los 7-Nov-19 9:24am
So what? A 'data' variable is created locally and never used...
There must be a "connection" between model (fixture) - view (XAML) - viewmodel (fixture view) in MVVM pattern. Think of it!
Please, refer this: How to: Create and Bind to an ObservableCollection | Microsoft Docs[^]
Member 14647867 7-Nov-19 9:26am
None of the solutions work for me in the first link, I tried the second on and when I changed 'Window1' to 'MainWindow' I got errors
Member 14647867 7-Nov-19 9:33am
Yes it currently creates the data variable but then I have no idea how to get it to the Main Window. Thats my question...
Member 14647867 7-Nov-19 9:33am
So currently it is never used

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100