Set default usercontrol property value in asp.net - c#

I have worked out how to set the default property value for a usercontrol in a windows form as below
private bool _MyTestProperty = true;
[DefaultValue(true)]
public bool MyTestProperty
{
get
{
return this._MyTestProperty;
}
set
{
this._MyTestProperty = value;
}
}
This code successfully causes a default value to appear in the properties pane when the usercontrol is being used inside a windows form.
However applying the same technique to usercontrols (ascx files) in ASP.NET does not work. Could anyone guide me in the right direction?

Related

How to persist custom user control property in application settings?

I created simple user control consisting of 3 elements:
2 radio buttons and table layout panel aka Yes or No control.
I created custom property boolean "Value" which changes depending of checked radio button.
UPDATE 1: I added that control to form and bind property "Value" to settings and in control code I added logic to determine which radio but should be checked but after saving settings and reloading form neither of radio buttons are checked.
How can I achieve that effect with the least effort.
Below the code:
public partial class YesOrNoControl : UserControl
{
public YesOrNoControl()
{
InitializeComponent();
LoadValue();
}
[Description("Sets the value of Control"), Category("Behavior"), DefaultValue(false), Browsable(true)]
public bool Value { get; set; }
void LoadValue()
{
if (Value)
{
YesButton.Checked = true;
}
else
{
NoButton.Checked = true;
}
}
private void YesButton_Click(object sender, EventArgs e)
{
Value = true;
}
private void NoButton_Click(object sender, EventArgs e)
{
Value = false;
}
}
You can define application settings within the IDE (under project settings).
You can then manipulate the settings by use of the Properties.Settings namespace.
Settings are automatically loaded at runtime, you can save the settings by calling the Save() method.
More links: Using Application Settings and User Settings
Applications Settings for WinForms
I managed to solve my problem.
I modified property "Value" to get value from application setting (specially created for this purpose) and set value to same application setting.
At the end of setter I added saving of application settings.
It solves the main problem but it's kind of workaround, not the true answer to problem.
Below the modified code of property:
public bool Value
{
get
{
return Properties.Settings.Default.YesOrNoControlValue;
}
set
{
Properties.Settings.Default["YesOrNoControlValue"] = value;
Properties.Settings.Default.Save();
}
}

Is there a way to access control properties within custom user control from Designer using c# (Windows Forms)

I have here some custom user control with DataGridView in it, now when i Implement my user control to some form I want to be able to access DataGridView properties within designer, is this possible?
This is my user control
public partial class MyUserControlTest01 : UserControl
{
// my way to accsses DataGridView
//
public DataGridView Dtv_userControl
{
get { return myUserControl_datagridView; }
set { myUserControl_datagridView = value; }
}
public MyUserControlTest01()
{
InitializeComponent();
}
So when i implement this user control to some Form, I can access DataGridView properties from code, but i want to do it from Designer.
Hope my question is clear,
any suggestion is helpful.
Thank you for your time.
Just add these annotations over your DataGridView-Property
[Browsable(true)]
[Category("*Any category you want*")]
public DataGridView Dtv_userControl
{
get { return myUserControl_datagridView; }
set { myUserControl_datagridView = value; }
}

Hide WinForm UserControl custom property from VS designer

Visual Studio is incorrectly calling my UserControl's custom properties at design time.
I have read many of the posting about using the [Browsable( false )] and [DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden )] attributes, but this has not worked for me.
To reproduce this problem, using Visual Studio, create a new Windows Forms Application, then add a User Control to your project, and drag that User Control onto your Form. Add a public custom property to your User Control, as shown below.
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
[Browsable( false )]
[DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden )]
public bool AreYouThere
{
get
{
MessageBox.Show( "Yes I Am Here!" );
return true;
}
}
}
When the Form is open in the Visual Studio designer, if I force the solution to clean and then rebuild, I will see a MessageBox with the text "Yes I Am Here!", indicating that Visual Studio has called the AreYouThere property on my User Control.
This should not happen, since I have decorated the AreYouThere property with the [Browsable( false )] and [DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden )] attributes.
Any idea why this is happening?
(This problem occurs on Visual Studio 2010 and 2013).
In order to hide a property from every place possible you have to mark it with those attributes
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[EditorBrowsable(EditorBrowsableState.Never)]
[Bindable(false)]
[Browsable(false)]
public class CustomDesigner : ControlDesigner
{
private static string[] RemovedProperties = new[]
{
"AccessibilityObject","AccessibleDefaultActionDescription","AccessibleDescription",
"AccessibleName","AccessibleRole","AllowDrop","Anchor","AutoEllipsis","AutoScrollOffset",
"AutoSize","AutoSizeMode","FlatAppearance", "FlatStyle",
"TextAlign","TextImageRelation","UseCompatibleTextRendering",
"UseMnemonic","UseWaitCursor"
};
public CustomDesigner() { }
protected override void PreFilterProperties(IDictionary properties)
{
foreach (string prop in RemovedProperties)
{
properties.Remove(prop);
}
base.PreFilterProperties(properties);
}
}
[ToolboxItem(true)]
[DesignerCategory("code")]
[Designer(typeof(CustomDesigner))]
public partial class NewButton : Button
{
public Color OnHoverBackColor
{
get { return _onHoverBackColor; }
set
{
_onHoverBackColor = value;
Invalidate();
}
}
}
Do not set the default value for the property as you want it. In your example, set the property AreYouThere to false/true and in the parent or whereever you are using it you instanceOfUserControl1.AreYouThere = true/false in say Load event.

How do I make a C# user control property data bindable?

I have a MVVM Windows Phone 8 app. The XAML page has a user control that I created that needs to be notified when a change takes place in the View Model. To facilitate this, I created an int property in the user control to be bound to a property in the View Model, so the user control property's setter method would be triggered when the property it was bound to in the View Model changed.
Using the code below, the user control's VideosShownCount property does show up in the Property List at design-time but when I click on the binding mini-button, the Create Data Binding option is greyed out in the pop-up menu.
So I have one or two questions, depending on what is the root problem:
1) How do I make a property in a View Model available as a Data Binding source?
2) How do I format a user control property so the IDE allows it to be data bound to a View Model property?
private int _videosShownCount = 0;
public int VideosShownCount
{
get
{
return this._videosShownCount;
}
set
{
this._videosShownCount = value;
}
}
public static readonly DependencyProperty VideoShownCountProperty =
DependencyProperty.Register("VideosShownCount", typeof(int), typeof(MyUserControl),
new PropertyMetadata(0, new PropertyChangedCallback(VideoShownCountPropertyChanged)));
static void VideoShownCountPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
MyUserControl MyUserUserControl = (MyUserControl)sender;
// Don't care about the value, just want the notification.
// int val = (int)e.NewValue;
// Do work now that we've been notified of a change.
MyUserUserControl.DoWork();
}
You're not using the DependencyProperty for your property, which will definitely cause problems between your code and the bindings
public int VideosShownCount
{
get { return (int) GetValue(VideosShownCountProperty); }
set { SetValue(VideosShownCountProperty, value); }
}
I'm not sure if this is the main cause of your problem, but it's worth fixing regardless.

dependent property does not change when changed via the view setter of a listview during design time

[Category("SomeCat")]
[Description("Gets or sets how items are displayed in the ShellListView control.")]
[DefaultValue(View.Details)]
new public View View
{
get { return base.View; }
set
{
System.Diagnostics.Debug.WriteLine("View");
if (value != View.LargeIcon)
{
//Reset these values because they can only be true if LargeIcon is set.
ShowExtraLargeIcons = false;
}
base.View = value;
}
}
private bool m_ShowExtraLargeIcons;
[Category("Appearance")]
[DefaultValue(false)]
public bool ShowExtraLargeIcons
{
get { return m_ShowExtraLargeIcons; }
set
{
if (m_ShowExtraLargeIcons == value)
return;
System.Diagnostics.Debug.WriteLine("Extra");
m_ShowExtraLargeIcons = value;
if (m_ShowExtraLargeIcons)
// Always set view to LargeIcon if ShowExtraLargeIcons is enabled
View = View.LargeIcon;
}
}
My problem: If I set View to something else than LargeIcons (via the property manager of VS 2010), the ShowExtraLargeIcons-property remains True although it has been set to False.
If I set the ShowExtraLargeIcons to True, the property View is set to LargeIcons as expected.
Something that might help: The Debug-messages ("View" and "Extra") after setting ShowExtraLargeIcons are shown, after setting View they are not (both set during design time).
This has nothing to do with dependency properties, it's simply the behavior of the property browser.
When you use the new modifier on a class member, you are not creating an "override". ListView.View is not a virtual property. You are creating a completely new property (MyListView.View) which has the same signature and name.
The property browser is going to enumerate properties and use descriptors to work with them. It will see two completely different properties and either display both of them, or pick one arbitrarily.
new public View
Looks like you are editing some parant object if trace is not shown. And that paranet object is edited without influencing m_ShowExtraLargeIcons var.

Categories

Resources