Pre-defined XAML code for new control that is added in VS designer - c#

Is there a way to automatically generate XAML code when you drop your control from toolbox to designer? I.e. it would create certain objects (nested properties) automatically and user would not have to type the same code every time to set certain (nested) properties. Kind of a template XAML code that is pre-defined by control's developer.
I'm pretty new to WPF so I'm wondering why there aren't any nested properties in the controls that are in Visual Studio's toolbox (button, label etc...)? Are attached properties a cure for this or have they just cut corners when designing WPF. :)
The problem is that my control (WinForms style property hierarchy) has nested properties and if I "internally" in my code create objects for those properties then XAML does not reflect the situation because it does not have any elements that match the current situation (i.e. the objects created in my control's constructor).
Is the only solution to leave all the properties null and let the user create them all? That way it seems to work correctly but user has to write many lines of XAML to reflect the situation in WinForms.

The functionality that you seek is not available via any of the controls in WPF. However, Visual Studio used to have Macros that would enable us to add pre-written sections of code into our pages, but unfortunately, they decided to remove that great functionality. Fortunately, they have introduced Code Snippets as a partial alternative.
Unfortunately again, these Code Snippets don't work in XAML pages, but once again fortunately, there are a few Visual Studio add ins that will enable you to enter pre-written XAML into your pages at the click of a button on the Code Plex website:
XAML Code Snippets addin for Visual Studio 2010
XAML Snippets for Visual Studio
Please try using one of the above add ins to see if they meet your needs.

Related

c# Binding windows forms controls in designer without typing any code

I need to bind two controls (pictureboxes) in my c# windowsforms designer (using Visual Studio 2015) so when I do an action with the parent one (for example hide it), the child will also hide... but on the other hand, when i hide the child, the parent will stay the way it is...is there a way to do this just in the designer without typing any code?
I couldn't find any answer on the Internet.
Thank you.
There's no way of doing this in the designer. You will have to implement this behavior using some code.

How does the VS editor know about controls in the XAML part

It almost seems like magic that as soon as I enter a control with a Name="myControl" attribute to the XAML code of a window, the editor of Visual Studio knows about the corresponding member variable in the code-behind. So the editor and intellisense seem to scan the XAML code at design time in order to anticipate which member variables the framework will create at runtime for the controls of a window. What exactly goes on behind the scenes for this to work? My guess is that VS uses the System.Windows.Markup.IComponentConnector interface at design time to find out about the properties of a window class.
When you are working with window/user control xaml in designer you are changing both: xaml and designer generated cs (look inside obj folder, there are WindowName.g.cs and WindowName.g.i.cs).
As soon as you add x:Name it will appears as a property in designer generated cs. Then you (or Intellisense) is able to use it when you are editing xaml.cs.

Visual Studio: Typing code vs. drag and drop from the control menu

I tried creating a gridview by typing code using another block of code on a different page as an example. I found that, when I ran the program and clicked the Edit button on a row, the fields did not change into textboxes for editing. I did have code to handle the Row Editing event.
In an earlier project I found that I had to double-click the Row Editing event in the Properties window for a gridview to create the event handling code. Typing in that code in the separate aspx.cs file did not work.
I am working with ASP.Net / C#.
What is going on with Visual Studio when I do drag and drop from the control menu as opposed to just typing in code? I am assuming it is adding something that I am missing when I am just typing.
Does anyone else have other examples that didn't work when they typed code that I should watch out for?
Even if you did a mighty good job copying the HTML, you likely still missed a few properties. That's because the GridView component comes with a bunch of so-called Design-Time properties that can (only) be configured from the Properties panel in Visual Studio.
I think you'll find that dragging a control from the toolbox onto your web form will set the control's design-time properties to a specific set of default values. This does not happen when you type the HTML by hand.

Show dynamically created controls in Windows Forms designer

Is there a way to see controls which are created via code in designer instantly but not only during execution?
The Windows Forms designer only applies properties contained in the automatically generated file "Form1.Designer.cs" (example filename for "Form1"). If you change properties (e.g. text, color, whatever) or create new controls in your own code, i.e. in "Form1.cs", the designer does not show it.
It is practically impossible because the designer would have to either 1) parse your code or 2) execute it to apply all changes to the controls.
Option 1 does not work because expression evaluation only works when running the code... Which leads us to option 2: Letting the designer running your code to find out dynamically added properties? First of all, automatically running untrusted code is not what you want. Second, there must be a reason that you do these changes dynamically instead of statically in the designer, so showing dynamic changes as WYSIWYG does not even make sense.
The designer can only display controls that exist at design time or show example controls for databound controls. If you think about code that would dynamically at runtime create a textbox or label based on a variable, how would the designer know which one to display in design mode?
If you have specific logic for how you want your dynamically created controls to display in design mode, you would have to create a custom control and implement the design time drawing code. This is mentioned under the Custom Design Experience heading here: http://msdn.microsoft.com/en-us/library/ms171725.aspx

How to hide the inner controls of a UserControl in the Designer?

I have a UserControl that contains a DropDownList and an ObjectDataSource. That control is used in the markup of my MasterPage.
In the Designer of a ContentPage that uses that MasterPage I can see the DropDownList and the ObjectDataSource. See for yourself.
I know that other controls (like ComponentArt:Grid) only show themself as outer container and hide the inner controls.
I guess that is somehow achievable with attributes. Which ones do I have to set?
I'm guessing that the control you refer to, ComponentArt:Grid, have an associated ControlDesigner which can do about anything, including designtime editable areas and custom action menus (the menu associated with the arrow in the upper right corner).
However, Visual Studio does not [last time I tried] execute the control designer for UserControls, so you can not easily control this. There's a lot of magic happening in the background in the UserControlDesigner including parsing the html code. However, I've never seen actual compilation of the codebehind file, where the ControlDesigner attribute would be specified.
You will need to rewrite your control as a server control for Visual Studio to check for the attribute, and allowing your custom designtime view.
I'm not very familiar with WebForms but the BrowsableAttribute might be what you are looking for. Also look at the DesignTimeVisibleAttribute.
The System.ComponentModel namespace has a number of other attributes that may do what you need.
Visual studio doesn't support this. The moment you specify the word "user control", the thing goes beserk and renders everything you put on it.
Fortunately you can influence how server controls are rendered. In other words, you can cheat the Visual Studio designer by making use of that. The code you need can be found here: http://www.codeproject.com/Tips/773145/Hiding-contents-in-ASP-Net-user-control-designer

Categories

Resources