Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
wpf question - access data in Action
#1
I have a sample "multi-tab" wpf window I'm trying to build.  I'd like to add buttons that *don't* result in the window being closed.  I've added an Action handler, but I'm having trouble accessing the rest of the form data from the wpf window in that action.  I'm a bit stuck.  In the below code, I'd really like to access "generic_t1.Text" in the method printIt(WBButtonClickArgs).  

I can find the form element using a w/elm find but that seems a bit hack-ish to me?  

Alternatively I think I discovered that if a button is inside a page and it has a button Id associated (it should set ResultButton) I don't get a return value for the buttons inside the page.  Only in the outside wpfBuilder.  

Again, my preference would be to use the Action approach and figure out a way to access the text boxes, combo boxes etc that are in the wpf page.

Sample code below:

C# code:
// script ""
using System.Windows;
using System.Windows.Controls;
script.setup(trayIcon: true, lockExit: true);

Action<WBButtonClickArgs> doAction = printIt;

var b = new wpfBuilder("Signal Magic").WinSize(400)
    .Row(-1).Add(out TabControl tc).Height(400)
    .End();

wpfBuilder _Page(string name, WBPanelType panelType = WBPanelType.Grid) {
    var tp = new TabItem { Header = name };
    tc.Items.Add(tp);
    return new wpfBuilder(tp, panelType);
}

_Page("Page One")
    .R.Add(out TextBlock generic_instructions)
    .R.Add(out Separator _)
    .R.Add("How Many", out TextBox generic_t1)
    .R.AddButton("go", doAction)
    .R.AddButton("stop", 22)
    .End();

generic_instructions.Text = "This will do some stuff";
generic_instructions.TextWrapping = TextWrapping.Wrap;


_Page("Page Two")
    .R.Add("Combo", out ComboBox _).Editable().Items("One|Two|Three")
    .R.Add(out CheckBox _, "Check")
    .End();

_Page("Page Three")
    .R.Add("Combo", out ComboBox _).Editable().Items("One|Two|Three")
    .R.Add(out CheckBox _, "Check")
    .End();

_Page("Page Four")
    .R.Add("Combo", out ComboBox _).Editable().Items("One|Two|Three")
    .R.Add(out CheckBox _, "Check")
    .End();

_Page("Page Five")
    .R.Add("Combo", out ComboBox _).Editable().Items("One|Two|Three")
    .R.Add(out CheckBox _, "Check")
    .End();

_Page("Page Six")
    .R.Add("Combo", out ComboBox _).Editable().Items("One|Two|Three")
    .R.Add(out CheckBox _, "Check")
    .End();

b.End();

b.ShowDialog();

// I could do a switch statement based on which button was selected,
// but it seems that the resultButton for the overall form isn't cascaded
// from inside a tab page?  I get zero every time I run this and click on the "stop" button
print.it(b.ResultButton);



void printIt(WBButtonClickArgs args) {
    var w = wnd.find(1, "Signal Magic", "HwndWrapper[Script1;*");
    var e = w.Elm["TEXT", "How Many"].Find(1);
    print.it(e.Value);
    
}


ok. .... after scratching my head on this.. I think I came up with a way.  Similar to the way I'd handle "global" variables in csharp.  Not sure this is the "right way", but it should work.  Comments on this approach:

C# code:
// script ""
using System.Windows.Controls;

Action<WBButtonClickArgs> doGoButton = printIt;

var b = new wpfBuilder("Window").WinSize(400);
b.Row(-1).Add(out TabControl tc).Height(300..);
var bMain=b;

wpfBuilder _Page(string name, WBPanelType panelType = WBPanelType.Grid) {
    var tp = new TabItem { Header = name };
    tc.Items.Add(tp);
    return new wpfBuilder(tp, panelType);
}

//--------------------------------

b = _Page("Page1");
b.R.Add("Text", out values.count);
b.R.AddButton("GO!", doGoButton);
b.End();
//--------------------------------

b = _Page("Page2");
b.R.Add("Combo", out ComboBox _).Editable().Items("Zero|One|Two");
b.R.Add(out CheckBox _, "Check");
b.End();

//--------------------------------

//tc.SelectedIndex = 1;

b = bMain.End();
if (!b.ShowDialog()) return;
print.it(b.ResultButton);

void printIt(WBButtonClickArgs args) {
    print.it(values.count.Text);
}

static class values {
    public static TextBox count;
}

#2
Examples.

C# code:
// script ""
using System.Windows.Controls;

var b = new wpfBuilder("Window").WinSize(400);
b.R.Add("How Many", out TextBox generic_t1).Focus();
b.R.AddButton("go", printIt);
b.End();
if (!b.ShowDialog()) return;


void printIt(WBButtonClickArgs args) {
    print.it(generic_t1.Text);
    
}


C# code:
// script ""
using System.Windows.Controls;

TextBox generic_t1 = null;

var b = new wpfBuilder("Window").WinSize(400);
b.R.AddButton("go", printIt);
b.R.Add("How Many", out generic_t1).Focus();
b.End();
if (!b.ShowDialog()) return;


void printIt(WBButtonClickArgs args) {
    print.it(generic_t1.Text);
    
}


C# code:
// script ""
using System.Windows.Controls;

var b = new wpfBuilder("Window").WinSize(400);
b.R.Add(out Button go, "go");
b.R.Add("How Many", out TextBox generic_t1).Focus();
b.End();

go.Click+=(_,_)=> { printIt(); };

if (!b.ShowDialog()) return;


void printIt() {
    print.it(generic_t1.Text);
    
}


C# code:
// script "" /// See menu File -> New -> Dialogs.

using System.Windows;
using System.Windows.Controls;

var d = new DialogClass();
d.ShowDialog();

class DialogClass : Window {
    TextBox generic_t1;
    
    public DialogClass() {
        Title = "Dialog";
        var b = new wpfBuilder(this).WinSize(400);
        b.R.Add("How Many", out generic_t1).Focus();
        b.R.AddButton("go", _ => printIt());
        b.R.AddOkCancel();
        b.End();
        
//        b.Loaded += ()=> {
//            
//        };

        b.OkApply += e => {
            print.it(generic_t1.Text);
        };
    }

    void printIt() {
        print.it(generic_t1.Text);
    }
}
#3
ButtonResult is 0 because in the tab-dialog code each page has its wpfBuilder.

C# code:
// script ""
using System.Windows.Controls;

var bMain = new wpfBuilder("Window").WinSize(400);
bMain.Row(-1).Add(out TabControl tc).Height(300..);
bMain.R.AddOkCancel(apply: "_Apply");

wpfBuilder _Page(string name, WBPanelType panelType = WBPanelType.Grid) {
    var tp = new TabItem { Header = name };
    tc.Items.Add(tp);
    return new wpfBuilder(tp, panelType);
}

//--------------------------------

var b1 = _Page("Page1");
b1.R.AddButton("Page1 result", 10);
b1.End();

//--------------------------------

var b2 = _Page("Page2");
b2.R.AddButton("Page2 result", 20);
b2.End();

//--------------------------------

bMain.R.AddButton("Main result", 100);
bMain.End();

if (!bMain.ShowDialog()) return;

print.it(bMain.ResultButton, b1.ResultButton, b2.ResultButton);
#4
Thanks for the reply.  I was getting an "undeclared" error when I tried to access TextBox.Text from inside the Action.  It's working fine now though.  So, I blame the guy between the chair and keyboard.

Thanks for the explanation on the result code.  I notice the sample code generated from a snipped in the editor assigns the _Page  to a variable.  I wasn't assigning it to anything.  That's fixed too!


Forum Jump:


Users browsing this thread: 1 Guest(s)