How can I bind to a programmatic property in .NET? - c#

I have a form with a programmatic property called SelectedAccessGroups:
[Bindable(true)]
public string SelectedAccessGroups
{
get { return "Selected Access Groups here"; }
}
I also have a BindingSource on the form which has a field called EditableByAccessGroups. I would like to bind my SelectedAccessGroups property to that field.
I attempted the following in my form's constructor, but it doesn't work:
this.DataBindings.Add(new System.Windows.Forms.Binding("SelectedAccessGroups",
this.CriteriaBindingSource, "EditableByAccessGroups"));
Is there a way to accomplish this?
Thanks!
Update: The error I was getting when attempting to run my program was quite undescriptive, but looking into it further, I found that I was getting the error because my property was read-only. I added a blank setter to the property, and the binding works fine now.

You have to implement INotifyPropertyChanged on your class so the bound items will know when something is different. here is an msdn article and there are several other articles out there that will help as well in implementing this interface.
http://msdn.microsoft.com/en-us/library/ms743695.aspx

Per your update and change of the question, to get one-way binding you could use use one of the other Binding constructors that takes DataSourceUpdateMode as a parameter.
http://msdn.microsoft.com/en-us/library/system.windows.forms.binding.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.datasourceupdatemode.aspx

Rather than add the cruft of a blank setter, try using the overloaded constructor of Binding that takes a DataSourceUpdateMode value and passing DatasourceUpdateMode.Never. This will prevent the binding from attempting to update the datasource from the control.

Related

How to programmatically change binding when datacontext changes

I have a custom control that allows the consumer to send in markup that will be parsed into Inlines, and it will try to match a given Command name with its appropriate ICommand. I have this working except in the case where the DataContext is not set yet. I know that storing the markup and reloading it upon DataContextChanged is not appropriate, but I cannot seem to find anything that works yet. I have tried BindingExpression and Binding to no avail as I do not see a way to attach them to a Hyperlink
I know that storing the markup and reloading it upon DataContextChanged is not appropriate
If you're going to be building a binding to ICommand instances within the DataContext, this is likely the only option that will make sense.
Otherwise, you'd never be able to correct the binding when the data context is changed.
Looking into some other code, I realized that this is all I needed to do:
hyperlink.SetBinding(Hyperlink.CommandProperty, new Binding(description.Command));
The WPF code picks it up generically...I will need to reflect to see exactly what it does, but it works. I could not set the Command property directly, but this worked :)

Is it possible to bind to a property name?

Is it possible to bind a property name. I seem to come up with run time errors when I try.
For instance:
<button Name="{Binding UniqueID}" Click="ButtonHandler">
This being in a header for a collection in a grid-view...
You can't bind Name, sorry. It's used for too many things internally and stuff would surely go crazy if you could. The docs are a bit vague, but do say this: (emphasis mine)
You cannot use the string value of Name as a
direct source value for a data binding source. If you have to display
the same string value as Name in UI with binding, you should replicate
the same value to the Tag property, which can be used as a property
binding source. Also don't use Name as a binding target.
(MSDN: FrameworkElement.Name)
However, if you want to attach random extra data to UI controls, I would recommend using attached properties instead. That way they're specifically associated with what you're doing and will be appropriately typed, unlike Tag.
(MSDN: Custom Attached Properties)
Well, I've read the documentation over and over and cannot find a way to make it work. The documentation doesn't say you cannot do it, but it doesn't say you can do it either.
However, I found two workarounds. Instead of binding the name, if you aren't using Tag or DataContect, you can find to those and in the handler extract them by casting as a string.
It's not elegant, but, it does seem to work as expected.

Winform: Binding a custom control property to a BindingList

I'm trying to create a binding from my custom control to objects that are in a BindingList.
While with textbox, I can easily write
textBox.DataBindings.Add("Text",myBindingList,"PropertyOfObjectOfBindingList")
With my custom property "Value", this thing doesn't work (the object doesn't get updated).
What should I implement with my custom control to make it works? I already implemented INotifyPropertyChanged, but it doesn't work.
I just want make this line works:
customControl.DataBindings.Add("CustomProperty",myBindingList,"PropertyOfObjectOfBindingList")
EDIT 1:
I read this around web: http://kbalertz.com/327413/control-using-Visual.aspx however is not working for me at the moment, maybe I'm doing something wrong
Since you said your bound object doesn't get updated (I assume from Control -> Object changes), but it is bound correctly, maybe this will help:
customControl.DataBindings.Add("CustomProperty", list, "BoundObjectProperty",
false, DataSourceUpdateMode.OnPropertyChanged);
Maybe the Implementing complex data binding in custom controls article will help.
I solved the problem by myself:
While the article I linked is a good suggestion, there is a wrong part; you don't have to create an event in your custom class with PropertyChangedEventHandler, but just with EventHandler.
public event EventHandler CustomPropertyChanged;
Is enough to make everything works. Obviusly you have to call it when your property changes
EDIT 1:
I discovered a bad thing, while on textboxes, if the control lose focus the bindinglist get updated, on my custom controls this thing happens only when I change selected item in listbox.
I don't find a way to solve this at the moment.

Bind list from FormView to model in ASP.net webforms

For a large fillin form I use the asp.net FormView for the magic databinding to my model. In this model I've a property called Rides (shown below), which exposes a list, which I obviously not want to be replaced entirely. So I made it readonly.
However, this way I can't use the databinding features anymore. Is there a common solution for this problem?
public IList<Ride> Rides
{
get
{
if (this._rides == null)
{
this._rides = new List<Ride>();
}
return this._rides;
}
}
You can still use the databinding, instead of <%# Bind("PropertyName")%> use <%# Eval("PropertyName")%>,
as Bind is usually for 2 way binding & Eval is usually for evaluation of the underlying datasources, Bind will be handy when you have a setter as well.
Hope this sheds enough light & you will find it helpful.
Monty,
Take a look at a class named BindingList. Binding list enables two-way binding. You can create it from Yor collection in code and bidn it to the datasource property of the FormView. I think this is what You want.
Also by exposing IList YOU have not made this thing read-only. I can recast to the List and I can modify You items all the way. If You really want to expose rides as read-only return IEnumerable and return not a List by a ReadOnlyCollection... recasting list to the other class wont' help.
Wild stab in the dark, don't know if this will work but I guess you can try...
How about putting the setter (set) back in the property, but make it assign to itself?
eg.
set
{
this._rides = this._rides; // Rather than this._rides = value;
}
This would mean the value is untouchable from the property, if a set operation is attempted it won't do any damage and should still bind...

ValueConverter with data binding

Is it possible to add a dependency property to a ValueConverter and bind it to a property of the object to which the converter is applied?
It isn't possible to do this in XAML. What you'd usually want to use for the sort of problem I'm guessing you have is a IMultiValueConverter.
If you really had to you could wire it up like you suggest in the code behind for your view but I really wouldn't recommend it. First it is much more difficult to manage and second your property won't be updated when the value you've bound to the new dependency property changes (i.e. the Convert method won't be called again). You're much better off using an IMultiValueConverter.

Categories