How can I bind certain properties belonging to a custom control? - c#

If I have the following control:
public partial class MyControl : UserControl{
public string MyControlText{
get { return MyTextBox.Text; }
set { MyTextBox.Text = value; }
}
public MyControl(){ ... }
}
How can I bind to the "MyControlText" property when I place the control on one of my pages, like so:
<local:MyControl MyControlText={Binding Path=SomeField} />
Thanks!

You need to make the property a dependency property. The documentation for the DependencyProperty class shows you how to do this:
http://msdn.microsoft.com/en-us/library/system.windows.dependencyproperty.aspx#

Related

Dependency Property Not showing in Property Pane

I attachted a Property to a WPF UserControl like this:
public partial class Datensatzliste : UserControl
{
public int Modulnummer
{
get { return (int)this.GetValue(ModulnummerProperty); }
set { this.SetValue(ModulnummerProperty, value); }
}
public static DependencyProperty ModulnummerProperty = DependencyProperty.Register("Modulnummer", typeof(int), typeof(Datensatzliste), new PropertyMetadata(0));
....
}
The Control is hosted in a WinForms application. When I select the Control in Visual Studio, the property is not shown. Ho do I geht the Property to show up in the property pane?
You could set the value of the property from the code-behind.

How to inherit ViewModel class to UserControl?

Suppose that I've a ViewModel class with different property such as:
//MainVm will inherit ViewModel that contains IPropertyChanged implementation
public class MainVM : ViewModel
{
publi bool Foo { get; set; }
}
I instatiated this class on the main controller in this way:
MainController mc;
public MaiWindow()
{
mc = new MainController();
DataContext = mc;
InitializeComponent();
}
the MainController have this implementation:
public class MainController : MainVM
{
some methods
}
so each time I need to access to a property of MainController on each UserControls I need to do: Application.Current.MainWindow.mc.Foo
and this is't elegant at all.
Is possible access to the property of specific ViewModel without call the code line above?
Thanks.
UPDATE
For add more details and clarification to this question: so in my UserControl I need to access to the Foo property that is part of MainVM. I'm spoke about the UserControl xaml code (not the controller of the UserControl), this is an example:
public partial class ControlName : UserControl
{
public ControlName()
{
InitializeComponent();
}
public btnAdd_Click(object sender, RoutedEventArgs e)
{
//Suppose that I need to access to the property Foo of MainVM here
//I need to access to MainWindow instance like this:
Application.Current.MainWindow.mc.Foo
//isn't possible instead access to the property directly inherit the class, like:
MainVm.Foo
}
}
In order to get the configuration App-Wide, you could use the
ConfigurationManager.AppSettings["Whatever"];
These settings are located in the App.Config in the following form
<appSettings>
<add key="Whatever" value="Hello" />
</apSettings>
But as I undertand, you have a ViewModel that let Users change the settings, in this case you should go for:
Properties.Settings.Default.myColor = Color.AliceBlue;
You ViewModel could expose this property as:
public Color MyColor
{
get {
return Properties.Settings.Default.myColor;
}
set {
Properties.Settings.Default.myColor = value; RaisePropertyChanged();
}
}
public void Persist()
{
Properties.Settings.Default.Save();
// Raise whatever needed !
}
From other ViewModels you can access these setting as well:
Properties.Settings.Default.myColor
Have a look here https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/using-application-settings-and-user-settings and here https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-create-application-settings

How to expose combobox Databindings on a usercontrol

I have a combobox on a usercontrol. I can expose the datasource however I cant expose the actual bindings.
If you add a normal combobox to a form and go to the databindings property you can choose selected value, text etc.
After this is chosen the designer automatically creates a
combobox.databindings.add("SelectedValue", datasource, columname, true));
How can I expose a combobox on a user control so that it has the above behavior
It's probably not considered best practice to expose your controls like this since after all, part of the point of using a UserControl is to hide the details of the child controls.
Try exposing the control on the UserControl as a property:
public partial class UserControl1 : UserControl {
public UserControl1() {
InitializeComponent();
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ComboBox ComboBox {
get {
return this.comboBox1;
}
}
}
If you are only interested in the control's DataBindings, then try to just expose that information:
public partial class UserControl1 : UserControl {
public UserControl1() {
InitializeComponent();
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ControlBindingsCollection ComboDataBindings {
get {
return this.comboBox1.DataBindings;
}
}
}

Custom dependency property isn't getting binded input

I'm creating a UserControl with a DependencyProperty but the DependencyProperty isn't getting the value that the caller is passing in.
I've discovered the following during my own investigation
If I use a built-in user control, such as TextBlock, everything works. This narrows the problem down to my UserControl's implementation (as opposed to the code that calls the UserControl)
My property changed callback that I register isn't even being called (well... at least the breakpoint isn't being hit)
If only see this problem when I use a binding to provide the dependency property, so this doesn't work:
<common:MyUserControl MyDP="{Binding MyValue}"/>
but I have no problems if I get rid of the binding and hardcode the value, so this works:
<common:MyUserControl MyDP="hardCodedValue"/>
Here's my UserControl's code behind:
public partial class MyUserControl : UserControl
{
public string MyDP
{
get { return (string)GetValue(MyDPProperty); }
set { SetValue(MyDPProperty, value); }
}
public static readonly DependencyProperty MyDPProperty =
DependencyProperty.Register(
"MyDP",
typeof(string),
typeof(MyUserControl),
new FrameworkPropertyMetadata(
"this is the default value",
new PropertyChangedCallback(MyUserControl.MyDPPropertyChanged)));
public static void MyDPPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
((MyUserControl)obj).MyDP = (string)e.NewValue;
}
public MyUserControl()
{
InitializeComponent();
this.DataContext = this;
}
}
And here's the xaml
<Grid>
<TextBlock Text="{Binding MyDP}"/>
</Grid>
Since I'm able to use built-in user controls such as TextBlock, I don't think that the error lies in my host code, but here it is, just so that you have a complete picture:
<StackPanel>
<common:MyUserControl MyDP="{Binding MyValue}"/>
</StackPanel>
public class MainWindowViewModel
{
public string MyValue { get { return "this is the real value."; } }
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainWindowViewModel();
}
}
This line in the UserControl is wrong:
this.DataContext = this;
This makes the UserControl its own DataContext, so the binding is looking for a property called MyValue on the UserControl, and that property does not exist. You want the DataContext to be your view-model. If you don't set it explicitly, it will inherit the DataContext from its container (the Window in this case).
Delete that line, and you'll be closer. You also don't need that callback; remove that too.
You can update your control's view code like that:
<Grid>
<TextBlock x:Name="_textBlock"/>
</Grid>
And set a _textBlock's text property in MyDPPropertyChanged method:
public static void MyDPPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var control = ((MyUserControl)obj);
control.MyDP = (string)e.NewValue;
control._textBlock.Text = control.MyDP;
}
That will do the trick.
Kindly Implement INotifyPropertyChanged, and PropertyChangedEventHandler in side the control and also the viewmodel. secondly use SetCurrentValue method to set the value inside the control class rather setting it directly

Create ToolTip for Custom UserControl

I need to understand how to utilize a ToolTip with a custom UserControl. Just creating the ToolTip on a form and assigning the specific control a ToolTip (via SetToolTip) obviously will not work.
What properties do I need to give the custom UserControl in order to assign ToolTip text to it? Do I need to add a ToolTip on the usercontrol form? How can I go about doing this?
Please provide a code sample or something for me to visualize.
Thank you!
Put a ToolTip on your UserControl (use the designer, just like you would put one on a form), and add a public property to your UserControl like:
public string TextBoxHint
{
get
{
return toolTip1.GetToolTip(textBox1);
}
set
{
toolTip1.SetToolTip(textBox1, value);
}
}
Create SetToolTip method in user control and set tooltip for each user control's subcontrol:
public partial class SomeUserControl : UserControl
{
public void SetToolTip(ToolTip toolTip)
{
string text = toolTip.GetToolTip(this);
toolTip.SetToolTip(subControl1, text);
toolTip.SetToolTip(subControl2, text);
// ...
}
}
Set tooltip text for user control instance in parent control designer. This adds in .designer file:
this.toolTip1.SetToolTip(this.someUserControl1, "Some text.");
Call SetToolTip method of user control instance with ToolTip parent control instance from parent control's constructor:
public partial class ParentForm : Form
{
public ParentForm()
{
InitializeComponent();
someUserControl1.SetToolTip(toolTip1);
}
}
This is the correct way to implement a serialized ToolTip property:
public partial class YourControlClass : UserControl
{
// Serialized property.
private ToolTip toolTip = new System.Windows.Forms.ToolTip();
// Public and designer access to the property.
public string ToolTip
{
get
{
return toolTip.GetToolTip(this);
}
set
{
toolTip.SetToolTip(this, value);
}
}

Categories