I want to databind a Dynamic object to a control in a winform app. So far I got it somewhat working, it does seems to databind "the first time", but then when I change the property value it does not take effect on the binded control, and that's the problem I can't overcome.
Here is the code, if you create a new winform app with a textbox and a button you can test it:
public partial class Form1 : Form
{
public dynamic ViewData { get; set; }
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.ViewData = new ExpandoObject();
this.ViewData.Test = "test1";
var bind = new Binding("Text", this.ViewData, null);
bind.Format += (o, c) => c.Value = this.ViewData.Test;
bind.Parse += (o, c) => this.ViewData.Test = c.Value;
textBox1.DataBindings.Add(bind);
this.ViewData.Test = "test2";
}
private void button1_Click(object sender, EventArgs e)
{
this.ViewData.Test = "test3";
}
}
For the record, if I change the control value, it'd reflected on the dynamic property, which is OK too.
For the sake of completeness, This post gave me this approach.
Simply, Binding cannot respond to your DataSource's property changes as it is not aware of the name of the property it needs to listen to, because the property name is not provided in Binding's constructor (3rd parameter) - by design.
To overcome this issue, basically, we need to listen to changes of DataSource and inform or force Binding to read value again. Fortunately, Binding class has a public method ReadValue, which forces Binding to read the value from DataSource again.
If you extend your code with the following line e.g. before binding is added to the DataBindings collection, that could solve the two-way bindings.
((INotifyPropertyChanged)this.ViewData).PropertyChanged += (sender2, e2) =>
{
if (e2.PropertyName == "Test")
{
bind.ReadValue();
}
};
At the end, all these workarounds can be encapsulated into a nice helper method to hide the details and make it reusable as much as possible.
Related
I have been trying to create a small form application and I wanted to try out binding a DataGridView directly to a collection of objects.
I created the following classes
public class MyClassRepository
{
public List<MyClass> MyClassList { get; set; } = new List<MyClass> { new MyClass { Name = "Test" } };
}
public class MyClass
{
public string Name { get; set; }
}
and I added the following code to a form to test. I based this off of the code in the designer after setting the BindingSource through the UI (while following this walk through https://msdn.microsoft.com/en-us/library/ms171892.aspx)
var tmp = new BindingSource();
tmp.DataMember = "MyClassList";
tmp.DataSource = typeof(MyClassRepository);
When this didn't work I started running through the code behind BindingSource to see what was happening. The setter calls ResetList which tries to create a dataSourceInstance by calling ListBindingHelper.GetListFromType. This call ultimately calls SecurityUtils.SecureCreateInstance(Type) where type is a BindingList<MyClassRepository>. This passes null to args which is passed Activator.CreateInstance which returns an empty collection.
After this ListBindingHelper.GetList(dataSourceInstance, this.dataMember) is called. This method calls ListBindingHelper.GetListItemProperties which results in a PropertyDescriptor for my MyClassList property and assigns it to dmProp.
At this point GetList calls GetFirstItemByEnumerable(dataSource as IEnumerable) where dataSource is the previously created (and empty) instance of BindingList<MyClassRepository> and returns (currentItem == null) ? null : dmProp.GetValue(currentItem);.
The value of dmProp/MyClassList is never accessed and the BindingSource is never populated with the instance I created. Am I doing something wrong? If not is there a bug in the source code? It seems to me like either SecureCreateInstance(Type type, object[] args) should be called and MyClassList should be passed via args instead of the existing call to SecureCreateInstance(Type type) or the value of dmProp should be used regardless?
If that is not correct how do I make the Designers automatically generated code set the DataSource to an instance of the object? Or do I have to inherit from BindingSource? If the latter why does it give you the option to choose a class that does not inherit from BindingSource?
As Reza Aghaei points out, in the designer, setting the BindingSource.DataSource to the “MyClassRepository” may work, however you still need to initialize (create a new) MyClassRepository object. I do not see this line of code anywhere in the posted code: MyClassRepository myRepositiory = new MyClassRepository(); The data source is empty because you have not created an instance of “MyClassRepository” and as Reza points out, this is usually done in the forms Load event.
To keep it simple, remove the DataSource for the BindingSource in the designer and simply set up the BindingSource’s data source in the form load event like below. First, create a new “instance” of the MyClassRepository then use its MyClassList property as a data source to the BindingSource. I hope this helps.
MyClassRepository repOfMyClass;
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
repOfMyClass = new MyClassRepository();
bindingSource1.DataSource = repOfMyClass.MyClassList;
dataGridView1.DataSource = bindingSource1;
}
Edit-----
After further review… I agree that you should be able to do as you describe. I was able to get it working as expected with the code below.
BindingSource bindingSource1;
private void Form1_Load(object sender, EventArgs e) {
bindingSource1 = new BindingSource();
bindingSource1.DataSource = typeof(MyClassRepository);
bindingSource1.DataMember = "MyClassList";
dataGridView1.DataSource = bindingSource1;
}
I followed the same steps in the “designer” and it worked as expected. Is there something else I am missing? As you stated… using MyClassRepository mcr = new MyClassRepository() appears to be unnecessary. In addition, if you cannot get it to work using one of the two ways above… then something else is going on. If it does not work as above, what happens?
Edit 2
Without creating a “new” MyClassRepository, object was unexpected and I did not realize that new items added to the list/grid were going into the bindingSource1. The main point, is that without instantiating a “new” MyClassRepository object, the constructor will never run. This means that the property List<MyClass> MyClassList will never get instantiated. Nor will the default values get set.
Therefore, the MyClassList variable will be inaccessible in this context. Example in this particular case, if rows are added to the grid, then bindingSource1.Count property would return the correct number of rows. Not only will the row count be zero (0) in MyClassList but also more importantly… is “how” would you even access the MyClassList property without first instantiating a “new” MyClassRepository object? Because of this inaccessibility, MyClassList will never be used.
Edit 3 ---
What you are trying to achieve can be done in a myriad number of ways. Using your code and my posted code will not work if you want MyClassList to contain the real time changes made in the grid by the user. Example, in your code and mine… if the user adds a row to the grid, it will add that item to the “bindingSource” but it will NOT add it to MyClassList. I can only guess this is not what you want. Otherwise, what is the purpose of MyClassList. The code below “will” use MyClassList as expected. If you drop the “designer” perspective… you can do the same thing in three (3) lines of code... IF you fix the broken MyClassRepository class and create a new one on the form load event. IMHO this is much easier than fiddling with the designer.
Changes to MyClassRepository… added a constructor, added a size property and a method as an example.
class MyClassRepository {
public List<MyClass> MyClassList { get; set; }
public int MaxSize { get; set; }
public MyClassRepository() {
MyClassList = new List<MyClass>();
MaxSize = 1000;
}
public void MyClassListSize() {
MessageBox.Show("MyClassList.Count: " + MyClassList.Count);
}
// other list manager methods....
}
Changes to MyClass… added a property as an example.
class MyClass {
public string Name { get; set; }
public string Age { get; set; }
}
Finaly, the form load event to create a new MyClassRepository, set up the binding source to point to MyClassList and lastly set the binding source as a data source to the grid. NOTE: making myClassRepository and gridBindingSource global variables is unnecessary and is set this way to check that MyClassList is updated in real time with what the user does in the grid. This is done in the button click event below.
MyClassRepository myClassRepository;
BindingSource gridBindingSource;
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
try {
myClassRepository = new MyClassRepository();
gridBindingSource = new BindingSource(myClassRepository.MyClassList, "");
dataGridView1.DataSource = gridBindingSource;
}
catch (Exception ex) {
MessageBox.Show("Error: " + ex.Message);
}
}
private void button1_Click_1(object sender, EventArgs e) {
MessageBox.Show("Binding source count:" + gridBindingSource.Count + Environment.NewLine +
"MyClassList count: " + myClassRepository.MyClassList.Count);
}
I hope this makes sense. ;-)
Designer sets DataSource = typeof(Something) for design-time support, for example to let you choose DataMember from a dropdown or to let you choose the data source property from dropdown while setting up data-bindings.
How do I make the Designers automatically generated code set the
DataSource to an instance of the object?
Forcing the designer to do that doesn't make much sense, because the designer doesn't have any idea about what the real data source you are going to use to load data. It can be a web service, a WCF service, a business logic layer class.
So at run-time you need to assign an instance of your list to DataSource. For example in Load event of the form.
I want to change the value that is assign to control of a form in c# (visual studio 2010), while the form is loaded.
I want my form should display to the end user, but at the same time as I get the data from server, I want it to reflect the same data onto the controls. (without any using timer, thread or any event).
Example : textBox1.text ="abc";
if server is sending "xyz" than while form is already loaded testbox's value should automatically change to xyz.
without any click or any kind of event.
You have to look at how Properties in c# work:
If we decompile a simple class on sharplab.io
public class C {
public int foo
{get;set;}
}
You will see that the compile will always generate backing fields and a getter and setter method.
So if you don't want to trigger an event you will have to bypass these methods as most likely the event will be triggered in there.
This should be doable with an reflection which is normally pretty easy to do.
BUT Textbox doesn't seem to have a backing field which is easily accessible for its Text-Property. Most likely it is set by its private StringSource field. Which is from the internal type StringSource. So first we have to get the type. Get a reference to the constructor then call this and set the private field.
This is how far i've come:
private int number = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
number++;
this.textBox1.Text = number.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
number++;
Type cTorType = typeof(string[]);
string[] cTorParams = new string[] { number.ToString() };
Type type = this.textBox1.GetType().GetRuntimeFields().ElementAt(11).FieldType;
ConstructorInfo ctor = type.GetConstructor(new[] { cTorType });
object stringSourceInstance = ctor.Invoke(new[] { cTorParams });
this.textBox1.GetType().GetRuntimeFields().ElementAt(11).SetValue(this.textBox1, stringSourceInstance);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
MessageBox.Show("Changed!");
}
I'd recommend digging a bit more into reflection and see what you can find in the TextBox class by using typeof(TextBox).GetFields / .GetProperties because somewhere there must be a field or property which you can change to bypass your setter method triggering the event.
Hope this helps.
Let's say I have a String property in my form (and the form does not implement INotifyPropertyChanged). I've also created a BindingSource and set its DataSource to the form. Then I bind a textbox to the String property on my form (indirectly, using the BindingSource).
Question 1: When I change the value in the textbox at runtime, why don't I hit a breakpoint in the setter of the String property? I thought that binding the control to the String property would allow updates in this direction (GUI -> member data) to occur automatically
Question 2: How can I trigger updates in the other direction (member data -> GUI) to occur when something other than the GUI changes the String property? I don't want to implement the INotifyPropertyChanged interface and add NotifyPropertyChanged to the setter. I thought that by using the BindingSource's ResetBindings I could at least trigger this manually
public partial class Form1 : Form
{
private String m_blah;
public String Blah
{
get
{
return m_blah;
}
set
{
m_blah = value;
}
}
public Form1()
{
InitializeComponent();
textBox1.DataBindings.Add(new Binding("Text", bindingSource1, "Blah",true,DataSourceUpdateMode.OnValidation));
}
private void button1_Click(object sender, EventArgs e)
{
Blah = "Clicked!";
this.bindingSource1.ResetBindings(false); //expecting the GUI to update and say "Clicked!"
}
}
this.bindingSource1.DataSource = this;
I think you forget to assign data source.
I have a custom control which is having a dependency property defined in it and my control implements INotifyPropertyChanged interface.
Dependency Property is Collection of Custom Objects.
Scenario 1
DP is of type List, whatever change I make in the list, nothing updated in MainUI, because I believe WPF does not understand adding and removing objects in list. it understands completely new references so to achieve this, whenever I want to update my list on control I use
MyProperty=new List();
In this approach, my DP callback fires everytime but eventArgs.NewValue always remains zero(it updated the list on UI correctly) but because I need to write some logic in property changed callback based on e.NewItems.Count, in this case that didn't work. Please tell me why e.NewItems does not work.
Scenario 2
DP is of type ObservableCollection, so as my collection property in view model against which I am binding my DP. in this case my property change callbacks does not fire at all, because I never use "new" keyword again after initialzing the property first time. UI updates but property change still not fires. So my logic in property change call back does not gets executed.
How should I make any of them or both of them working.
I would use the ObservableCollection approach, and subscribe to it's CollectionChanged event.
That way you will get notified whenever the collection has been changed.
But the other approach should work as well. When you set the regular list to a new instance, the PropertyChangedCallback will be fired for the dependency property, and by examining the DependencyPropertyChangedEventArgs object you can get the new value.
XAML:
<StackPanel>
<Button Content="Add to observablecollection" Click="click1" />
<Button Content="Set list to new instance" Click="click2" />
</StackPanel>
Code-behind:
public partial class Window1 : Window
{
public ObservableCollection<string> Strings { get; set; }
public List<string> StringsList
{
get { return (List<string>)GetValue(StringsListProperty); }
set { SetValue(StringsListProperty, value); }
}
public static readonly DependencyProperty StringsListProperty =
DependencyProperty.Register("StringsList", typeof(List<string>), typeof(Window), new PropertyMetadata(null, StringsListPropertyChanged));
public Window1()
{
InitializeComponent();
Strings = new ObservableCollection<string>();
Strings.CollectionChanged += strings_CollectionChanged;
StringsList = new List<string> { "Test1", "Test2", "Test3", "Test4" };
}
void strings_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
//Fires everytime the observablecollection has an item added/removed etc.
MessageBox.Show(string.Format("ObservableCollection has changed! Count is now {0}", this.Strings.Count.ToString()));
if (this.Strings.Count == 10)
Console.WriteLine("Collection contains 10 strings!!");
}
private static void StringsListPropertyChanged(DependencyObject e, DependencyPropertyChangedEventArgs args)
{
var newCount = ((List<string>)args.NewValue).Count.ToString();
MessageBox.Show(string.Format("Dependency property has changed! Count is now {0}", newCount));
}
private void click1(object sender, RoutedEventArgs e)
{
this.Strings.Add("Test1");
}
private void click2(object sender, RoutedEventArgs e)
{
this.StringsList = new List<string> { "Newitem1", "Newitem2" };
}
}
ObservableCollection inherits from both INotifyPropertyChanged and INotifyCollectionChanged. I think if you want to know when the collection changed you should use this interface:
INotifyCollectionChanged
Hi i have 2 listviews in a grid. Each listview contains a textbox and both textboxes in both listviews have the same text. When i select part of the text in either textbox, the other textbox will select the same part of the text as well.
can binding between two textbox in 2 different listview be done?
As AngelWPF writes, the Selection*-properties are not dependency properties so you cannot use databinding on them.
What you can do though, is to add your own subcalss of the TextBox that has dependency properties that replace the original properties. These can be implemented as regular dependency properties using the same names as the original properties, but the definition of them must be public new to replace the originals.
I will not post an entire code sample here (too much code and I don't have it on this computer), but you can do something like the following:
public class BindableSelectionTextBox : TextBox
{
// Defines the dependency property as normal
public static readonly DependencyProperty SelectedTextProperty =
DependencyProperty.RegisterAttached(SelectedText, typeof(string),
typeof(BindableSelectionTextBox),
new FrameworkPropertyMetadata("", SelectedTextPropertyChanged));
private static void SelectedTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var textBox = (TextBox)d;
textBox.SelectedText = (string)e.NewValue;
}
public new string SelectedText
{
get { return (string)GetValue(SelectedTextProperty); }
set
{
if(value != SelectedText)
{
SetValue(SelectedTextProperty, value);
}
}
}
public BindableSelectionTextBox()
{
SelectionChanged += OnSelectionChanged;
}
private void OnSelectionChanged(object sender, RoutedEventArgs e)
{
SelectedText = base.SelectedText;
}
}
Now, you must repeat this for the SelectionStart and SelectionLength properties and you should be done.
Sadly because SelectionText, SelectionLength and SelectionStart are not dependency properties, the two textboxes canot be two way bound on these properties.
You will have to write an attached behavior, attach one textbox to another and handle TextBox.SelectionChangedEvent for both, and synchronize the other when the event is handled.