Passing Parameters from one XAML page to another - c#

I have a Tile Layout Control with of course Tiles in it which I use to let user navigate through pages, I simply want to pass some parameters like strings or integers between my pages... How to do that?

you can try this approach or just by passing data between classes
http://msmvps.com/blogs/siva/archive/2007/05/11/storing-application-wide-data-the-wpf-way.aspx
Application.Current.Properties["youvalueindex"];

Generally you will have some sort of manager controlling your windows. That manager would handle passing the data between the windows. The windows generally should not know anything about each other.
Simplistic Example
class Manager
{
...
void DoSomething()
{
ViewModelA vma = new ViewModelA();
WindowA wa = new WindowA();
wa.DataContext = vma;
wa.ShowDialog();
ViewModelB vmb = new ViewModelB();
vmb.SharedData = vma.SharedData;
WindowB wb = new WindowB();
wb.DataContext = vmb;
wb.ShowDialog();
}
...
}

Related

Bot Detect Captcha Set multiple image styles

Is there a way to set multiple image style for bot detect captcha?
I am referring to this sample.
I have written an extension class to initialize captcha,
public class CaptchaHelper
{
public static MvcCaptcha GetTicketsCaptcha()
{
// create the control instance
MvcCaptcha myCaptcha = new MvcCaptcha("ExampleCaptcha");
myCaptchaImageStyle = BotDetect.ImageStyle.AncientMosaic;
}
}
I can set one style, but cannot find a way to set multiple options. Need some help?
There is a way to do this, "CaptchaRandomization.GetRandomImageStyle" accepts ImageStyle array.
ImageStyle[] allowedStyles =
{
ImageStyle.AncientMosaic,
ImageStyle.MeltingHeat,
ImageStyle.MeltingHeat2,
ImageStyle.Negative
};
myCaptcha.ImageStyle = CaptchaRandomization.GetRandomImageStyle(allowedStyles);
More details

Passing data between user controls in wpf

I have a user control with a button which when clicked opens a new user control.
private void Button_Click(object sender, RoutedEventArgs e)
{
Window window = new Window
{
Title = "Window2",
Content = new UserDataControl2()
};
window.ShowDialog();
}
I need to pass a collection to the new user control. How can I do it?
The easiest way is to create a custom constructor for your user control.
// Button_Click event
Window window = new Window
{
Title = "Window2",
Content = new UserDataControl2("My Data");
};
// User Control class.
string _info;
public UserDataControl2(string info)
{
_info = info.
};
You could also create a method or property in the user control to receive the data as well. Use whichever seems more appropriate in your context.
The best way is passing object to DataContext of this Window. For this you will need to create a class where store this parameters (ViewModels) and after "binding" to the Window (View). After you can pass this object assigning to Datacontext.
Look to MVVM model to understand better what I mean.
MVVM Pattern Made Simple
MVVM in Depth

Form passing null value

I have fixed my form load problem. I changed it to where the main menu wasn't being called on load event and that fixed the issue. Now my retrieve event gets the version but never passes it to my form.
Here is my code for transferring process:
Where the information is being pulled from:
public string VersionPass { get; set; }
VersionPass = rtxtBoxNewVersion.Text;
This is the main menu where the value will be stored till they click the assign button. This is where it gets the value from the form.
public string VersionNum { get; set; }
VersionEditor newV = new VersionEditor();
newV.ShowDialog();
VersionNum = newV.VersionPass;
newV.Dispose();
Form being transferred to I am using form load because the value will not change: It never get the value into the PassedVersion = passedVersion.VersionNum; field.
MainMenu passedVersion = new MainMenu()
string PassedVersion;
private void Notification_Load(object sender, EventArgs e)
{
PassedVersion = passedVersion.VersionNum;
rTxtBoxVersion.Text = PassedVersion;
}
Having to guess a bit here, but ...
1) Take out new MainMenu() from the Notification_Load call. This is probably stopping the dialog being created properly.
2) If you want to share information you either need to pass it to the new object, for example when you create it:
MyObject = new MyClass(SomeObjectToTellItAbout);
// or maybe like this:
MyObject.InterestingInfo = SomeOtherObject;
If it's truely global information (app version, for example) you could make the info to share static (and then access it like this: MainMenu.MyAppVersion).
Edit based on comments:
You want to get an understanding of classes vs objects. A class is just a design; it's a concept. For example "human". An object is an instantiation of that class/concept/design, for example me. And you (another object). I can't find your name by saying h = new human(); h.name() and nor can you find your version by making a new MainMenu and asking it what version it is! Hope that helps.

how to avoid rebinding grid each time the control is initialized?

I've created a custom user control with a grid. I'd like to bind this grid once, and use it over and over again in my app. If I put the binding within the control, the data is retrieved as many times as I use the control. How do I bind it only once??
public ClientLookUp()
{
InitializeComponent();
vw_clientsTableAdapter.Fill(dsclientlkup.vw_clients); //This occurs as many times as I have the user control, instead of just once.
}
Well anything you put in the constructor will be executed every time you construct the object!
What about providing an Initialize method that you can call whenever you need to reload the data??
If you want to load the data only once, then load it either into a static variable or a separate class that is referenced by the control.
If you really want to use the same single grid in your control over and over, you could create a single, static grid, and have your ClientLookUp constructor add it to the right place—Panel, or whatever—whenever a new one is created.
Before you go do this road however, ask yourself if this is really what you want to do. Having the same identical grid existing in many places may cause you problems down the road. If you want to support in-grid editing, you'll find that changing one value changes the identical value in all your other grids..
EDIT
I tried getting the below code to work, but I'm not sure this approach will be possible. It seems as though the minute you try to attach the same UI element into more than one place, it gets moved out of the last place you put it; it doesn't look like you can have the same grid being in more than one place at once. This makes sense when you think about it.
Here's the code I tried. Maybe it will be of some use to you.
public UserControl1()
{
InitializeComponent();
this.Controls.Add(myStaticGridView);
myStaticGridView.Dock = DockStyle.Fill;
}
static DataGridView _staticGrid;
public DataGridView myStaticGridView
{
get
{
if (_staticGrid != null)
return _staticGrid;
_staticGrid = new DataGridView();
_staticGrid.Columns.Add("A", "A");
_staticGrid.Columns.Add("B", "B");
_staticGrid.Columns.Add("C", "C");
_staticGrid.Columns[0].DataPropertyName = "A";
_staticGrid.Columns[1].DataPropertyName = "B";
_staticGrid.Columns[2].DataPropertyName = "C";
_staticGrid.DataSource = new[] {
new { A = "someA", B = "someB", C = "someC"},
new { A = "someA", B = "someB", C = "someC"},
new { A = "someA", B = "someB", C = "someC"},
new { A = "someA", B = "someB", C = "someC"},
};
return _staticGrid;
}
}
And then loading the control like this:
private void button1_Click(object sender, EventArgs e)
{
flowLayoutPanel1.Controls.Add(new UserControl1());
}

Click a custom control and show some variables related to it in another control

in my Win Forms app I create an array of dynamic custom controls inside a loop. These, lets call them 'boxes', are like my basic pieces of information. I also create string arrays in other parts of the code that contain the information of this 'boxes', so that for example string[3] is a variable of box[3] and so does stringa[3], stringb[3], stringc[3]... all the arrays with the same index are related to the box with that index. Hope I make myself clear.
Only 2 of this strings are shown in 2 labels inside each custom control 'box' in the array, but the others are there because I want to make something so that when the user clicks one of these controls the other strings can be shown in another control. Sort of something like "More Information...". All the 'boxes' in the array need to have the same event handler because I create +100.
To put it more into context, each custom control 'box' in the array shows the Symbol and the Price of a stock and I want that when the user clicks on each stock more quote information is shown on another special control which is like a placeholder for "More info".
I am thinking of 2 ways to do it:
If I could "detect" the index of the clicked control (which is the same in the strings related to it), I could just set this to an int j and all I have to do is show all the strings a,b,c... with index j. Unfortunately I cannot find a way to do this, maybe it is not even possible.
The other way I have thought is to create some properties for my custom control which "store" this variables, and in my app instead of assigning strings I would set properties for each control, which I could later retrieve when the control is clicked. I haven't tryed this because I don't know exactly how to do it.
What do you think? Do you know how can I achieve this or do you have a different idea that will work? Please help! Thanks in advance.
It's kind of a broad implementation question since there are countless ways you could implement something like this.
If you are creating two collections, one with the buttons and one with the information, you potentially could just assign each of the buttons 'Tag' properties to point to the corresponding info and assign a generic OnClick event handler that displays the info.. something like:
infoControl.text = ((InfoClass)((Button)Sender.Tag)).pieceOfInformation;
But again there are many ways to do this, and the choice comes down to how you store your information.
For your first method, you could have a property of your custom control that is the index.
public class Box : Control
{
// ...existing code
private int index;
public int Index
{
get
{
return index;
}
set
{
index = value;
}
}
}
OR
For your second method, you could have a property of your custom control that is the additional info string.
public class Box : Control
{
// ...existing code
private string extraInfo;
public string ExtraInfo
{
get
{
return extraInfo;
}
set
{
extraInfo = value;
}
}
}
In either case, you could then access the proper information right in your click handler for the "box".
i don't know about the first way - got to noodle around more, but in the second way you can extended your custom or built-in control: for example:
public class ExtendedLabel: Label
{
public string[] MoreInfo { get; set; }
}
and initialize it
public TestForm()
{
InitializeComponent();
ExtendedLabel label = new ExtendedLabel();
label.MoreInfo = new string[] { "test" };
this.Controls.Add(label);
label.AutoSize = true;
label.Location = new System.Drawing.Point(120, 87);
label.Name = "label1";
label.Size = new System.Drawing.Size(35, 13);
label.TabIndex = 0;
label.Text = label.MoreInfo[0];
}
And later in your event handler you can use the inside information

Categories