I’m new to C# and WPF and have a problem I hope you can help with.
I have a WPF application with a stackpanel to which I add usercontrols by code to display the information stored in an instance of a class. The user can create instances, and musty be able to edit and delete instances of the class by selecting the usercontrol. The usercontrol contains a canvas for making a simple sketch as part of the information stored in the instance.
My problem is: How do I make the usercontrol selectable, so that the user can select an instance for editing or deleting, and so that I can address the canvas by code in a particular instance?
I hope you can help me.
/Morny
Related
I am a beginner in C# and Windows Forms, and I am trying to make a little application to manage different databases.
To do so, I would like to have a TabController with different TabPages, one for each database.
I already have a template of what will be inside of the TabPages, and I want to replicate it for every TabPage. So I would like to have a C# class for every TabPage so I can just copy/paste the template and change the data source.
The problem is Forms keeps creating Partial classes when I try to create a new form, or won't let me create a class binded to my TabPage.
Is there any way to have a TabPage have its own code ?
Thanks in advance.
Create a UserControl containing your database UI and a public "DataSource" property. Then, place one of the user controls on each tab page.
I have a xaml which contains a tab control (Name="MyTabControl"). I'm a beginner with wpf and in code and I want to dynamically add tab items that I then add a list box to each created tab item. Below is the code I have so far.
ListBox listbox = new ListBox()
TabItem tab = new TabItem()
tab.AddChild(listbox)
MyTabControl.Add(tab)
My issue is that I can't figure out how dynamically create new tabs that also would add a list box to each new tab and the new tabs then added to MyTabControl.
Then, I would want to be able to access each list box control, individually, in each tab to edit the list box content.
How is this done in code? How can i access the created list box controls to edit them?
WPF/UWP and XAML are designed with the MVVM pattern in mind. While you can use other approaches, doing so will miss about 90% of it's power and run into issues at every other corner.
In MVVM this would be simply a mater of Exposing a Collection and having a Tempalte targetting that type. ListBoxes might even have a custom Template system, but using ListBoxes might no longer be nessesary - any container can expose a Collection.
If you plan on learning MVVM, mid to longertem you should learn MVVM. I wrote a short intro a few years back, that should help you going. Help for people not following MVVM is parse on the Forum.
In general, it's a violation of the MVVM principles WPF is built around to build a UI in this way. Instead, consider a solution similar to the one proposed in the answers to this question. They do a good job of explaining both what to do, and why we do it this way.
I've created user control which hosts datagridview and other controls.
Then I drop it onto a form.
How do I allow myself to customize grid's properties (like which columns are shown) in a target form?
I thought setting its modifier to public will suffice.
That should do it, then you can address the grid through your user control instance. Assuming you control is named "MyControl" and your grid within the control is named "MyGrid" then you should be able to use MyControl.MyGrid. to get to the properties.
You can add properties to your UserControl that helps you to change design of your Control from different forms.
Problem not solved in that general way I initially posed it.
As a quick hack I declared public properties for some of the grid properties I needed (like Columns collection)
Tnanx for your help, though.
I'm creating a twitter client in C#.
I want to put every tweet as an element of a listbox.
I created a windows form that represents a tweet(it has a picture, and labels).
My problem is that when i can't see the tweets when i add them to the listbox. After adding 3 tweets(windows form objects), the listbox has 3 blank elements in them, but i can't see anything of it.
How can i add a windows form object to a listbox?
(these forms are working fine, because i can see them if i use the ShowDialog method)
please help
You can add a Form object to the ListBox.Items collection but the only thing you'll ever see is the type name of the form. ListBox is not capable of rendering controls as its items.
The efficient solution is to implement the ListBox.DrawItem event and custom draw the tweet. There's a good example of such an event handler in the MSDN Library documentation for the event.
The slow solution is to add controls to a Panel's Collection property with its AutoScroll property set to true. That cannot be a form, it must be a UserControl.
You may want to look into implementing it using WPF. Where you can pretty much put anything inside a listbox.
Some thoughts:
You'd do better to work with custom controls, rather than a whole form
A listbox can't have controls on it... consider a ListView: http://www.codeproject.com/KB/list/ListViewEmbeddedControls.aspx
Some example code of what you're trying will help us zoom in on what you're doing
HTH,
James
I don't think ListBox can display anything but a text string for each element, so you won't be able to see an image of each form if that's what you were hoping for. You might try using FlowLayoutPanel instead to manage a list of controls.
I am new to C# .NET.
Can some one help me out with the below problem:
I have a TabControl in my WindowsForm application, where the tab pages are generated dynamically. The content to be displayed on each tab would be fetched from my database. I need some kind of control (which can display the fetched data) that I can add on each tab page (which would be same for all tabs) such that I can associate some kind of event, say click, on that added control.
Can anyone tell me how to do this programmatically & write the click event for all the controls added?
Please refer the below link! You will get more detail in this regard.
Creating a tab control with a dynamic number of tabs in Visual Studio C#
I'm not sure I completely understand your problem but my initial thoughts are that you could dynamically create a datagrid or something similar for each tab that you are dynmically creating. You could then bind the datasource for the grid and then add the grid as a control to your tabpage.
Something like...
DataGridView gv = new DataGridView();
gv.DataSource = //whatever your source is
this.tabPage1.Controls.Add(gv);
You would then have all the events associated with the grid to work with.
I'm thinking data binding is going to be your best bet for displaying this information. You can create a list of objects and use a DataTemplate to format the data. You can apply the DataTemplate to a quite a few objects. I generally use the ItemsControl and ListBox
http://msdn.microsoft.com/en-us/library/ms750612.aspx
good luck