if I add control in Microsoft Blend 4 without set Name to this control and I want to set name to it and use it in c# how ?
example I added button using Blend in my layout but without give it a name
I want to give it a name using c# without x:Name="" in xaml
In your place I would give LogicalTreeHelper.GetChildren (this) a chance. It returns a collection of children to Window (this is a handle to Window) Reference MSDN
From there you can try to find your control.
But I think it is easier to try to rewrite the control (or look for another component) so you can have names on the children. That was your problem from the start.
Hope it helps
Gorgen
First, why in the world would you want to do that?
If you do not set a name you have no easy way of accessing the control. However you can get access to the control via relationships to other controls or events that pass a reference, for example the loaded event.
e.g.
private void Menu_Loaded(object sender, RoutedEventArgs e)
{
(sender as Menu).Name = "MainMenu";
}
Or if the control is the child of another control:
(ControlStack.Children[0] as Menu).Name = "MainMenu";
But i cannot think of anything useful that could be achieved by that...
You probably just want to get a reference to the object which you can easily store in a class member. In some cases you can also slice up your XAML using resources.
e.g.
<local:SomethingIWouldLikeToReference x:Key="SomethingIWouldLikeToReference"/>
<local:UserControl x:Name="userControl">
<Stuff>
<MoreStuff Content="{StaticResource SomethingIWouldLikeToReference}"/>
</Stuff>
</local:UserControl>
public MainWindow()
{
InitializeComponent();
MyReference = FindResource("SomethingIWouldLikeToReference") as SomethingIWouldLikeToReference;
}
Example if I have ListView Control and I want to use it to add items and remove items
Make private ListView and initialize it
ListView temp_control_List = new ListView()
then make loaded Eventhandler from Blend so it will be in VS then
private void ListView_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
temp_control_List = sender as ListView;
}
Now you can add and remove to and from the list view control from temp_control_List
Related
I am trying to create a nested DataGridView control where there will be two levels of nesting that are open at all times. It will look similar to the picture on this page: http://www.codeproject.com/Articles/12657/GridView-inline-Master-Detail-record-display. The difference will be that each subnesting will always be open and it is not necessary to have a way for the user to open/close each nesting. This control will only be used for displaying data, so there will be no need to modify the data directly from this control (even though the user will not modify the data directly it can still be changed).
If this can not be done with DataGridView, is there any other control that would allow for this.
If not does anyone know another way to do this. I can, but they would be tedious to implement. One way would be to add multiple DataGridView controls in sequence (2N DataGridControls for N categories). The other would add it all manually with static controls.
I don't know if this will give the answer, but it might make u some door.
//button to call function that looks for DatagridView control
private void button2_Click(object sender, EventArgs e)
{
scanDG(this);
}
private void scanDG(Control parent)
{
foreach (Control ctrl in parent.Controls)
{
if (ctrl.GetType().Name == "DataGridView")
{//If current Control is Datagridview then set Readonly to true
((DataGridView)ctrl).ReadOnly = true;
}
//If a control can contain control scan it and look for Datagridview control
if (ctrl.HasChildren) scanDG(ctrl);
}
}
I have a windows form and i dont want to make any other windows forms just one windows form and different user controls how can i change between user controls for example hide one and show the other user control programmatically ?
private void Btt_info_Click(object sender, EventArgs e)
{
Frm_Main frm_main = new Frm_Main();
frm_main.Controls["panel1"].Controls.Clear();
UC_Info uc_info = new UC_Info();
frm_main.Controls["panel1"].Controls.Add(uc_info);
}
i added this but it doesnt work
Add a container control (if I remember correctly, there's a containers section in the toolbox?), like a panel. Create usercontrols for what you want to dynamically switch around. So make like a 'HomePage' usercontrol and a 'LoginPage' usercontrol. Dynamically add the usercontrol that you want to display to the container. WHen you want, remove it from the container and add a different usercontrol:
Panel myPanel = new Panel();
LoginPage ctlLoginPage = new LoginPage();
HomePage ctlHomePage = new HomePage();
//add the loginpage to the panel first
myPanel.Controls.Add(ctlLoginPage);
...do stuff...
//remove whatever control is currently in the Panel
myPanel.Controls.Clear();
//add the other control, the HomePage control instead now
myPanel.Controls.Add(ctlHomePage);
..do other stuff...
I usually do it this way so you leave your form itself open to add common controls and stuff that might be shared between your different 'pages'.
EDIT: Note that I normally would add the panel in the designer and not create it dynamically in the code. This was just an example.
EDIT: The interaction between your mainform and usercontrols can be handled in a few different ways, and I am not saying that any of these is the correct method.
You create a static property for your Panel on the Mainform, so that
you can always access it to swap your controls around.
In this example I'll also add a static method for it
enum PanelControlsEnum {HomePage, LoginPage};
public static Panel MyContainerPanel {get;set;}
public static void SwitchPanelControls(PanelControlsEnum selControl){
..put your switch panels code here..
}
Then inside your usercontrol you call a predefined method, something like:
MainForm.SwitchPanelControls(PanelControlsEnum.HomePage);
Another method is to bind the button click event on your mainform
instead of inside the form.
Like This:
HomePage ctlHomePage = new HomePage();
ctlHomePage.Click += MyClickEvent;
myPanel.Controls.Add(ctlHomePage)
...
private void MyClickEvent(object sender, RoutedEventArgs e)
{
..switch user control code here...
}
Create a method that returns a UserControl object. Then put conditions in that method as to which control you want to load at a specific condition and then in your main form code.
UserControl control = GetControlFromMyMethod();
form1.Controls.Add(control);
where 'control' is the returned control from your method.
To remove the existing one you have to loop through the form1.Controls and find out the control and call 'Remove'.
Update:
Mike C has a better idea of adding a panel and loading your desired control on the panel as then it's easy to remove your control and you then don't have to loop through the forms controls to find it and then remove it.
Try this:
this.Controls.Clear();
usercontrol load = new usercontrol ();
this.Controls.Add(load);
load.Show();
you could try this it will definitely help you as it did helped me a lot it short and straight to the point hope that will help
Im using wpf and I want to pass a paramter of some textbox.text string on button click from my main window to my user control contained in my appPages folder, how can I achieve this?
The app control I want to send it to is called FindCurrentStudent, I dont like asking for direct code but I cant really find anything on this?
The way I was thinking was something like:
private void btnGeneral_Click(string _value, object sender, RoutedEventArgs e)
{
string value = textBox1.Text;
AppPages.FindCurrentStudent v1 = new AppPages.FindCurrentStudent(value);
//does not contain a constructor?
value = _value;
And then from the customcontrol I could call it like so:
string MainWindowValue = value;
There are various ways to achieve this. Depending on your current design, you might:
bind Button.Command to a command exposed by your UserControl, and Button.CommandParameter to the TextBox.Text
bind a property exposed by your UserControl to TextBox.Text
bind to properties on your view model(s)
You'll need to give more information on your current design before I could helpfully elaborate.
I have build 2 customized control.Control A and Control B
These controls are placed in more than 1 forms.
Let Suppose From A and Form B
Now let suppose user click the Control A in Form A.And this control click event is invoked.
Now how it would be identified that control A is located in Form A.
So that I can change the specific attribute of Control B of Form A.
You can use Control.Parent recursively. Also you can use Control.TopLevelControl if you don't have nested forms. Then you need to recursively walk Controls properties to find your second control.
But more proper solution of your task is to create a property in the first control and set it to the second control and use it in the event handler.
Handle Control A 's event inside each form ..
What version of C# are you using? If you're using a relatively new version and are using Windows Presentation Foundation (WPF), you can do something like this:
private void ControlA_MouseDown(object sender, MouseButtonEventArgs e)
{
((Grid)((ControlA)sender).Parent).Children
// The above line is INCOMPLETE, you need to select the right child, but
// you're at least in the right grid; The rest is up to you (depends on
// the setup of your program);
}
There is another 'hack' option you can use, which is by far the easiest. You can use a controls 'tag' property to store whatever you like, so for example a pointer to ControlB.
ControlA a = new ControlA();
ControlB b = new ControlB();
a.Tag = b;
Note that this is a hack, and the proper way is to create such a field / variable on the custom control yourself. It's more or less a demo of how it could work.
The answer of Akram Shahda is only an option if your forms are not dynamically created, since then you can predefine all actions. Nevertheless I wouldn't go that way.
First off, I'm new to WPF and C# so maybe the issue I have is really easy to fix. But I'm kinda stuck at the moment.
Let me explain my problem.
I have a WPF Window and two usercontrols (Controls and ContentDisplayer).
The usercontrol Controls, wich contains some buttons, is added in the XAML of the Window.
Nothing special here.
Window.XAML
<nv:Controls/>
Now, what I want to do is when a user is pressing a button in Controls, ContentDisplayer needs to be added to the Scatterview I have in my Window.
I solved the problem by adding the buttons to the Window, and not using the usercontrol Controls. But this is not what I want.
Window.XAML.CS
private static void Button_ContactChanged(object sender, ContactEventArgs e)
{
object ob = Application.LoadComponent(new Uri(
"NVApril;component\\XAML\\ContentDisplayer.xaml",
System.UriKind.RelativeOrAbsolute));
//Set a unique name to the UserControl
string name = String.Format("userControl{0}",
SurfaceWindow1_Scatterview.Items.Count);
UserControl userControl = ob as UserControl;
userControl.Name = name;
//Add the new control to the Scatterview
SurfaceWindow1_Scatterview.Items.Add(userControl);
SurfaceWindow1_Scatterview.RegisterName(name, userControl);
}
So the real question is: How do I add a usercontrol to the Window by pressing a button in an other usercontrol?
Thanks,
Toner
At the top of the window xaml add
xmlns:d="clr-namespace:SomeNamespace.Usercontrols"
where you these exist already, you can choose the namespace of your control from the intellesence list.
Then where you want to place the control type:
<d:yourusercontrolhere params />
and your usercontrols can be added there.
Within Controls expose an event that is fired when you want to add a new control.
public event EventHandler AddControl;
private void RaiseAddControl()
{
if (AddControl!= null)
{
AddControl(this, EventArgs.Empty);
}
}
Now sink that event in your Window
yourControl.AddControl += ContactChanged
In your window, it sounds like you need to add the event to the instances of Controls.
<local:ContentDisplayer>
...
<nv:Controls AddControl="ContactChanged"/>
...
Then in your ContactChanged event handler you can instantiate a new Controls control and add it to whatever collection you're using like in your Button_ContactChanged event handler above.
Let me know if you need further clarification.
I have no idea what you are trying to do your example,
So you have a control defined thus:
public partial class somecontrolname : UserControl
With your corresponding Xaml file
All you need to do to add it in code to your window is firstly you need a LayoutRoot such as Grid control in the window then just
[mylayoutcontrol].Children.Add(new somecontrolname());
Maybe I got wrong idea what you are trying to do, your example code doesn't make much sense to me, looks like you are trying to load the xaml source file