Hi all, I must admit I'm rather stumped by this one. I have a program that handles tickets/prizes for a business. Patrons obtain tickets, and then go to turn them in at a counter. An employee counts their tickets, and inputs that number into my program. The program then displays a list of prizes, filtered by ticket value. For example, under the Ticket Level 2 might be a pen and a coaster. Ticket level 5 might contain a t-shirt. Anyway, the users select how many of certain prize they want. The program then subtracts the appropriate number of tickets from the total.

I have this program working as a WinForm, by using variables (i.e. TicketsLeft) and a two column DataGridView, filtered by a ComboBox.

Here is an example of the existing WinForms program:
http://www.daniweb.com/forums/attachment.php?attachmentid=20127&stc=1&d=1300759795

Well, it worked great until the client decided to move to the flashy interface of WPF.

I'm rewriting the code, so it fills a "Customer" class with the information that used to go in the Form Variables, but I've hit a snag. I can't seem to access the underlying customer class to copy data back from the DataGridView.

Here is the working WinForms Code. (Pardon the Visual Basic):

If DataGridView1.Rows.Count <> 0 Then
            For I = 0 To DataGridView1.Rows.Count - 1
                For J = 0 To UBound(PrizeList)
                    If PrizeList(J, 0) = DataGridView1.Rows.Item(I).Cells(0).Value AndAlso PrizeList(J, 2) = ComboBox2.SelectedItem Then
                        If Not IsNumeric(DataGridView1.Rows.Item(I).Cells(1).Value) Then Me.GroupBox1.BackColor = Color.Red : GetLocalError(True) : Exit Sub
                        PrizeList(J, 1) = DataGridView1.Rows.Item(I).Cells(1).Value
                        Exit For
                    End If
                Next
            Next
        End If
        TL = TW
        For I = 0 To UBound(PrizeList)
            If PrizeList(I, 1) = "0" Then Continue For
            If Not IsNumeric(PrizeList(I, 1)) Then Me.GroupBox1.BackColor = Color.Red : GetLocalError(True) : Exit Sub
            TL = TL - (CInt(PrizeList(I, 1)) * PrizeList(I, 2))
        Next

And, here is my rewritten C# WPF code:

XAML:

xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"

<!-- Other code here -->                            <wfi:WindowsFormsHost Margin="0,65.54,0,0">
                                <wf:DataGridView x:Name="myDG">
                                </wf:DataGridView>
                            </wfi:WindowsFormsHost>

And heres the C# code:

void myDG_CellEndEdit(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
        {
            
        }

Thats it. Stuck. Ideally I would have this:

WPF.Customer.Prizelist[ //Implementation of the same VB logic from above
Attachments example.PNG 6.21 KB

I want to make sure I'm not missing something here....

You have a class (Customer - perhaps implemented in "Customer.cs") that holds the data for a Customer/PrizeList. You want to be able to access this class in the event handler?

Can't you just declare a variable in the class (class global), then create a new instance of the class in the "Window_Loaded" event of the form (and do whatever you need to do to load the data)? You can then use this instance down in the event handler for "CellEndEdit"? Something like this:

public partial class MainWindow : Window
{
    private Customer c_oCust = null;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        c_oCust = new Customer("Bob");
    }

    private void cmdOK_Click(object sender, RoutedEventArgs e)
    {
        if (c_oCust.TestCust(textBox1.Text))
        {
            MessageBox.Show("Yay!");
        }
        else
        {
            MessageBox.Show("The name in the object is: " + c_oCust.ToString());
        }
    }
}

That is, if the variable holding the instance of the "Customer" class is stored at the class level, any event handlers/methods should be able to see it.

Hopefully I'm not off the mark here and this helps.

I think I understand, its just, this is my first "real" WPF app, and I am totally confused by it. lol.

Ok, thanks to your lead (and my C# book) I think I figured it out. I needed:

Customer myC = BindingOperations.GetBinding(ProgramBox, ComboBox.SelectedValueProperty).Source as Customer;
This article has been dead for over six months. Start a new discussion instead.