How to expose whole sub control of usercontrol at winforms designer - winforms

For example
I want to create a usercontrol(windows form) that contains a lable and a textbox .
And I want to expose the two sub control as property , so that I can
set the sub control's property in client form designer.
so the code maybe like this :
public partial class LabelTextbox : UserControl
{
public LabelTextbox()
{
InitializeComponent();
}
[
Category("Appearance"),
Browsable(true),
Description("innerLabel")
]
public DevComponents.DotNetBar.LabelX LabelPart
{
get
{
return this.labelx;
}
set
{
this.labelx = value;
}
}
[
Category("Appearance"),
Browsable(true),
Description("InnerTextbox")
]
public TextBox TextBoxPart
{
get
{
return this.textboxx;
}
set
{
this.textboxx = value;
}
}
}
and then I can see it in designer , it looks like :
but when I set the usercontrol's inner label property in designer ,
it can't make relation code in the designer.cs .
that's to say the client settings not be saved .
so how can I resolve this problem.
By the way I come from CN my English is poor . Anyone can answer me.

Decorate the properties of your child Controls with the DesignerSerializationVisibility Attribute:
[
Category("Appearance"),
Browsable(true),
Description("innerLabel"),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content) //<-- here
]
public DevComponents.DotNetBar.LabelX LabelPart {
get {
return this.labelx;
}
set {
this.labelx = value;
}
}

Related

Style WindowsForms Project

I want to style my forms and some controls on my project, e.g. dataGridView.
I know, I can make a method like this an set it for every dataGridView.
public static void StyleDataGridView(DataGridView dg)
{
dg.BorderStyle = BorderStyle.FixedSingle;
dg.RowHeadersVisible = false;
....
}
My question: is there a possibility to do such style code only once in my project? How?
First step with userControl worked very well.
But how to handle the Click, DoubleClick and the CellMouseDown Event?
My code so far:
public partial class ucGridView : UserControl
{
public object DataSource
{
get { return dataGridView.DataSource; }
set { dataGridView.DataSource = value; }
}
public ucGridView()
{
InitializeComponent();
}
}

Databinding not refreshed in Xamarin (WPF)

I have some trouble with the data binding while using MVVM in Xamarin. Let me explain my architecture:
I have a Manager-class, which contains a ObservableCollection with classes of type t, t is my model class. The Manager-class contains also a attribute called activeT, which is the current selected object of my model class. There are 2 UIs, one which shows the current data of t. The viewModel is bound to the attribute t of my Manager-class like that:
public t CurrentT
{
get
{
return _mgr.CurrentT;
}
set
{
_mgr.CurrentT = value;
OnPropertyChanged();
}
}
_mgr is my singleton Manager-Object.
Now there is the other view, which is able to choose the current t out of a combobox. The viewModel of that view is bound to the ObservableCollection of the manager. If I change the selected object, I do it like with the same code like above. The Property of the manager is the following code:
public t CurrentT
{
get
{
return _currentT;
}
set
{
_currentT= value;
OnPropertyChanged());
}
}
The problem is now, that the first view to view the current selected t does not refresh, though I can see in the debugger, that the current t is changed by the other view.
Can someone help me?
Edit:
I provide some more Code:
The Manager-Class:
public class Manager : INotifyPropertyChanged
{
private t _currentConstructionSite;
private ObservableCollection<t> _constructionSites = null;
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(name));
}
public t CurrentConstructionSite
{
get
{
return _currentConstructionSite;
}
set
{
_currentConstructionSite = value;
OnPropertyChanged("CurrentConstructionSite");
}
}
public ObservableCollection<t> ConstructionSites
{
get
{
return _constructionSites;
}
set
{
_constructionSites = value;
}
}
private Manager()
{
ConstructionSites = DataRepository.GenConstructionSites();
_currentConstructionSite = ConstructionSites[0];
}
}
The ViewModels Class A (This is the viewmodel of the view, which shows some data):
public class DashboardViewModel : ViewModelBase
{
private Manager _mgr;
public t CurrentConstructionSite
{
get
{
return _mgr.CurrentConstructionSite;
}
set
{
_mgr.CurrentConstructionSite = value;
OnPropertyChanged();
}
}
public DashboardViewModel()
{
_mgr = Manager.getInstance();
}
}
The View A to show some data:
Binding Setup from XAML:
<ContentPage.BindingContext>
<local:DashboardViewModel x:Name="viewModel"/>
</ContentPage.BindingContext>
Binding a Label to show data:
<Label Text="{Binding CurrentConstructionSite.ConstructionSiteName, Mode=TwoWay}" HorizontalOptions="Center" Font="Bold" FontSize="Large"/>
ViewModel B to choose the current t:
public class ChooseConstructionSiteViewModel : ViewModelBase
{
Manager _mgr = null;
public ObservableCollection<t> ConstructionSites
{
get
{
return _mgr.ConstructionSites;
}
}
public t CurrentConstructionSite
{
get
{
return _mgr.CurrentConstructionSite;
}
set
{
_mgr.CurrentConstructionSite = value;
OnPropertyChanged();
}
}
public ChooseConstructionSiteViewModel()
{
_mgr = Manager.getInstance();
}
}
The View to choose the current t:
<combobox:SfComboBox x:Name="combobox" Grid.Row="0" Grid.Column="1" Margin="8,0,20,0" VerticalOptions="Center" HeightRequest="40" DataSource="{Binding ConstructionSites}" DisplayMemberPath="ConstructionSiteName" SelectionChanged="Handle_SelectionChanged"/>
And if the selection from the combobox changed:
void Handle_SelectionChanged(object sender, Syncfusion.XForms.ComboBox.SelectionChangedEventArgs e)
{
t selectedItem = e.Value as t;
_viewModel.CurrentConstructionSite = selectedItem;
}
The two views are contained as contetPages in a tabbedPage. It works in general, but the changing the selected t in the view B does not update the data in view A. I can see in the debugger that the value of t is changed via view B but when I go back to view A there is the old value. In the debugger I can see that the value is updated.
BTW: ViewModelBase is the class which implements INotifyPropertyChanged
From you second viewmodel, you need to "notify" the first viewmodel that the data has changed.
One of the ways to do that, would be to use a Messenger (MvvmLight has one, so does MvvmCross). You can then use the messenger from your Manager, to notify all the viewmodels that need the info, that CurrentT changed.
In the messenger subscription in your viewmodels, simply call a RaiseNotifyPropertyChanged of your property, and you should be good to go

How do I access different form's object for my windows form app

I have like 2 or 3 different forms and for example for my mainForm, I would like to access the object in settingsForm. How do I do that.
You need to expose that object, via a public property on settingsForm.
e.g.
In your settings form:
public Object MyObject
{
get { return myobject; }
}
then, on your main form, your can say;
settingsForm sf = new settingsForm();
sf.Show();
...
Console.Write(sf.MyObject.Text);
So. let's say settingsForm has a textbox that stores a value you want.
If you need access to the entire text box, you'd add a property on settings forms....
public TextBox textbox1
{
get { return textbox1; }
}
then, any form that instantiates and uses settingsForm, can use textbox1.
If you only want to access the value in textbox1, you'd only expose its Text property.
public string TextBoxValue
{
get { return textbox1.Text; }
}
public partial class mainForm : Form
{
settingForm settingObject;
public mainForm(settingForm settingObject)
{
InitializeComponent();
this.settingObject= settingObject;
}
}
the above code shows a simple way of how you access the object.

Silverlight datagrid bind nested object property

I am totally new to Sivlerlight world, so I need to know a simple thing.
how do I bind nested object as Item Source to the DataGrid.
I have a class Employee which looks Like :
Public class Employee
{
public long EmployeeId
{
get
{
return this._employeeId;
}
set
{
this._employeeId = value;
}
}
public string EmployeeName
{
get
{
return this._employeeName;
}
set
{
this._employeeName = value;
}
}
public tblDepartment tblDepartment
{
get
{
return this._tblDepartment;
}
set
{
this._tblDepartment = value;
}
}
}
now the class "tblDepartment" has Department name as its one of the properties, so what I want to achieve is show EmployeeId, EmployeeName and Department name in the DataGrid of Silverlight. I am using SilverLight 4.0.
Issue is I am not able to find how to bind Nested objects properties(i.e. Objects with in Objects).
can anyone help me out here,
thanks in advance.
You should be able to do
{Binding Path=EmployeeObject.tblDepartment.Name}

Binding Custom Object Datagrid Silverlight 4

I create an object Custom as you can see below
public class GridViewModel
{
private List _listRowCust = new List();
public List ListRowCust
{
get { return _listRowCust; }
set { _listRowCust = value; }
}
}
public class RowCustom
{
private List<CellCustom> _listCellCustom = new List<CellCustom>();
public List<CellCustom> ListCellCustom
{
get { return _listCellCustom; }
set { _listCellCustom = value; }
}
public RowCustom() { }
}
I try to bind the custom object on the datagrid object available in silverlight4.
I wish to bind any cell on the datagrid. One line should identify by a row object and each cell by a cellCustom.
I use this code
textColumn = new DataGridTextColumn();
textColumn.Header = "RemainingWork";
textColumn.Binding = new Binding("Cell[0]"); //it's a supposed syntax possibility in fact I have 3 rows with 10 cells
GridElements.Columns.Add(textColumn);
GridElements.ItemsSource = e.GridViewModel.ListRowCust;
I don't find any explanation on How to custom the binding.
Do you have any idea?
Thank you
best regards,
Alexandre
I think you can only bind to public properties.
So CellCustom must have a public property to bind to, if that's the object used for the itemsSource, or data context.

Resources