I am using the VS2017 IDE, and DotNet4.5.2 and WinForm frameworks.
I want to display a value with a progress bar, so I wrote the following code and it works:
progressBar1.DataBindings.Add(nameof(ProgressBar.Maximum), source, nameof(source.Maximum));
progressBar1.DataBindings.Add(nameof(ProgressBar.Minimum), source, nameof(source.Minimum));
progressBar1.DataBindings.Add(nameof(ProgressBar.Value), source, nameof(source.Status)).Format += (sender,e) => {
e.Value = (int)(e.Value as IStatus).Value;
};
I want to use the ToolStripProgressBar to display it, but because it cannot perform data binding, I defined a new class to inherit it and implement IBindableComponent as follows:
public class BindableToolStripProgressBar : ToolStripProgressBar, IBindableComponent
{
private BindingContext bindingContext;
private ControlBindingsCollection dataBindings;
public BindingContext BindingContext
{
get
{
if (bindingContext == null)
{
bindingContext = new BindingContext();
}
return bindingContext;
}
set
{
bindingContext = value;
}
}
public ControlBindingsCollection DataBindings
{
get
{
if (dataBindings == null)
{
dataBindings = new ControlBindingsCollection(this);
}
return dataBindings;
}
}
}
Then I changed the data binding object from ProgressBar to the newly defined BindableToolStripProgressBar, but this time running the application results in the throwing of a System.FormatException:
tspb.DataBindings.Add(nameof(ProgressBar.Maximum), source, nameof(source.Maximum));
tspb.DataBindings.Add(nameof(ProgressBar.Minimum), source, nameof(source.Minimum));
tspb.DataBindings.Add(nameof(ProgressBar.Value), source, nameof(source.Status)).Format += (sender, e) => {
e.Value = (int)(e.Value as IStatus).Value; // Will be thrown FormatException
};
Is there anything wrong with my implementation of IBindableComponent?
Thanks in advace!
ToolStripProgressBar has a ProgessBar property which exposes the ProgressBar control which is hosted in the ToolStripControlHost. Maximum, Minimum and Value properties of the ToolStripProgressBar basically get or set corresponding property of the underlying ProgressBar.
So you can setup data-binding to the underlying ProgressBar:
Source source = new Source() { Minimum = 0, Maximum = 100, Value = 50 };
private void Form1_Load(object sender, EventArgs e)
{
var p = toolStripProgressBar1.ProgressBar;
p.DataBindings.Add(nameof(p.Maximum), source, nameof(source.Maximum));
p.DataBindings.Add(nameof(p.Minimum), source, nameof(source.Minimum));
p.DataBindings.Add(nameof(p.Value), source, nameof(source.Value));
}
Related
This is my first experience with ToggleSwitch. What I am trying to achieve is to show different data from the list using different ToggleSwitches.
I have a ListView with multiple TextBlocks and one ToggleSwitch for each row of data.
Then I populate ListView with data from List. (List is populated using class that forsees
public ToggleSwitch Switch {get; set;}
Here is how I try to get ToggleSwitch data from each row:
private void ToggleSwitch_Toggled(object sender, RoutedEventArgs e)
{
for (int a = 0; a < jointList.Count; a++)
{
jointList[a].Switch = sender as ToggleSwitch;
if (jointList[a].Switch != null)
{
if (jointList[a].Switch.IsOn == true)
{
ToggleTest.Text = jointList[a].ProductId.ToString();
ToggleTest.Visibility = Visibility.Visible;
}
else
{
ToggleTest.Visibility = Visibility.Collapsed;
}
}
}
}
Unfortunately I am getting the same(last added) productId from all of the ToggleSwitches as if they were pointing to same place.
EDIT>
I have rewritten the code as touseef suggested:
private void ToggleSwitch_Toggled(object sender, RoutedEventArgs e)
{
for (int i = 0; i < jointList.Count; i++)
{
if (jointList[i].Value == true)
{
ToggleTest.Text = jointList[i].ProductId.ToString();
// ToggleTest.Text = jointList[a].ProductId.ToString();
ToggleTest.Visibility = Visibility.Visible;
}
else
{
ToggleTest.Visibility = Visibility.Collapsed;
}
}
}
But now nothing shows up.
EDIT:
Here is another attempt to resolve the problem:
private void ToggleSwitch_Toggled(object sender, RoutedEventArgs e)
{
foreach (var record in jointList)
{
if (record.Value == true)
{
ToggleTest.Text = record.ProductId.ToString();
ToggleTest.Visibility = Visibility.Visible;
}
else
{
ToggleTest.Visibility = Visibility.Collapsed;
}
}
}
And now only one ToggleSwitch works, the one that corresponds to the last added record (I was pulling ProductId of the jointList).
None of the other ToggleSwitches work. They don't return any data when using the code above.
Please use DataTemplate to populate a listview and within your datatemplate put a toggleswitch, and x:Bind the IsOn value for your toggleswitch with a bool property within your item's class. and to get the correct values in your c# object behind set two way databinding.
basic databinding : https://learn.microsoft.com/en-us/windows/uwp/data-binding/data-binding-quickstart
binding in depth : https://learn.microsoft.com/en-us/windows/uwp/data-binding/data-binding-in-depth
you can bind with Binding or x:Bind the better way to bind in uwp is x:Bind though, see the links I provided you they will help you a lot :)
Instead of looping the list and getting sender as ToggleSwitch which will obviously give u same instance everytime. you should just loop over the list which you bind to the ListView, and then check your item.IsOn property and get ur item.ProductId and do whateer u want to with ur item object. note that this item came from the List of items which u are binding to the ListView. when u set two way databinding with toggleswitch, your item.IsOn property will automatically change when toggleswitch.IsOn changes, so you don't need to get any instance of toggleswitch in ur code.
INotify
in order to get notified about the propertychange and for two way databinding to work properly, you need to inherit your Product class from following class
public class Observable : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void Set<T>(ref T storage, T value, [CallerMemberName]string propertyName = null)
{
if (Equals(storage, value))
{
return;
}
storage = value;
OnPropertyChanged(propertyName);
}
protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
and then in your IsOn property setter method, call onpropertychanged event like this.
public class Product : Observable
{
public int ProductId { get; set; }
private bool isOn;
public bool IsOn
{
get { return isOn; }
set { isOn = value; Set(ref isOn, value, nameof(IsOn)); }
}
}
Toggled Event
private void ToggleSwitch_Toggled(object sender, RoutedEventArgs e)
{
//assuming productList is a List<Product> which was set as ListView.ItemSource
foreach (var product in productList)
{
if (product.IsOn == true)
{
ToggleTest.Text = product.ProductId.ToString();
ToggleTest.Visibility = Visibility.Visible;
}
else
{
ToggleTest.Visibility = Visibility.Collapsed;
}
}
}
if your problem still isn't solved, I will recommend you to put a simplistic app with this problem on the GitHub repo and share the link in your question so people can have a detailed look at it.
I am having trouble understanding why my databindings do not seem to work with my custom class. I made (hacked) my class extend the Control class to add the databindings functionality but it doesn't actually bind to my custom property.
My code for my custom class is:
public class RadioButtonSet : System.Windows.Forms.Control
{
private Dictionary<System.Windows.Forms.RadioButton, int> buttonList;
private int selectedValue;
public RadioButtonSet()
{
buttonList = new Dictionary<System.Windows.Forms.RadioButton, int>();
}
public void AddButton(System.Windows.Forms.RadioButton button, int buttonValue)
{
if (this.buttonList.ContainsKey(button))
throw new Exception("Button set already contains specified button");
else if (buttonValue <= 0)
throw new Exception("Cannot add specified key to button set");
else if (button == null)
throw new Exception("Parameter button cannot be null");
else
{
button.CheckedChanged += button_CheckedChanged;
this.buttonList.Add(button, buttonValue);
}
}
private void setSelectedButton()
{
this.buttonList.FirstOrDefault(x => x.Value == this.selectedValue).Key.Checked = true;
}
private void button_CheckedChanged(object sender, EventArgs e)
{
System.Windows.Forms.RadioButton btn = sender as System.Windows.Forms.RadioButton;
this.selectedValue = this.buttonList[btn];
}
public int SelectedButton
{
get
{
return selectedValue;
}
set
{
selectedValue = value;
setSelectedButton();
}
}
}
And I try to bind to this class using the following, where rbs_admin is an instance of my custom class:
rbs_admin.DataBindings.Add("SelectedButton", datatable, "admin");
I do not know what information may help so here goes.
I get the information to bind from a datatable which is populated by a data adapter. This custom class is not in it's own file, its part of another static class in my project.
I just dont understand as I created a custom textbox with the same custom property and it binds and works fine.
Any help is much appreciated.
Im talking about something like this:
someListControl.DataSource = datatable;
someListControl.DisplayMember = "someAnotherColumnName"
rbs_admin.DataBindings.Add("SelectedButton", datatable, "admin");
Then, selecting an item from list control will cause your control to update its binding according to the selected item.
I have a bindingSource in winforms as well as a controller class.
I want to be able to set the selected record from within the controller class using 2 way binding.
That is If the form is displaying and I set the SelectedPerson in the controller then the bindingSOurce should make that person the current record.
My controller code is
public class PeopleController : BaseController
{
private SortableBindingList<Person> _blvPersons;
public SortableBindingList<Person> BlvPersons
{
get
{
return this._blvPersons;
}
set
{
this._blvPersons = value;
this.SendChange("BlvPersons");
}
}
private Person _selectedPerson;
public Person SelectedPerson
{
get
{
return this._selectedPerson;
}
set
{
this._selectedPerson = value;
this.SendChange("SelectedPerson");
this.SendChange("BlvPersons");
this.Trace("## SelectedPerson = {0}", value);
}
}
public void InitBindingList
{
using (var repo = new PeopleRepository(new OrganisationContext()))
{
IList<Person> lst = repo.GetList(p => p.Id > 0 && p.Archived == false, x => x.Organisation);
this.BlvPersons = new SortableBindingList<Person>(lst);
} }
}
//ect
}
public class BaseController : INotifyPropertyChanged, IDisposable
{
public event PropertyChangedEventHandler PropertyChanged;
public void SendChange(string propertyName)
{
System.Diagnostics.Debug.WriteLine("PropertyChanged {0} = {1}", propertyName, GetType().GetProperty(propertyName).GetValue(this, null));
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
// etc
I have a bindingSource on my form and set bindingSource.DataSource = controller.BlvPersons
If I Update data values using the controller I will see these changes in the form.
However I cant work out how to set the current record in the controller and see the change in the form.
You can use BindingSource.Find method and set the Position property to the results of the Find method.
The Find method can only be used when the underlying list is an
IBindingList with searching implemented. This method simply refers the
request to the underlying list's IBindingList.Find method.
To implement search on a generic BindingList requires various steps. First, you have to indicate that searching is supported by overriding the SupportsSearchingCore property. Next, you have to implement the IBindingList.Find method, which performs the search.
You can use examples from here or here.
Because I don't want a winforms reference in my controller class, I don't want to share the bindingSource between the form and the controller.
Instead I came up with the idea of having a RecordPosition property in the controller and binding it to a textbox
In my form I have
BindHelper.BindText(this.textRecordPosition,this.controller,"RecordPosition");
private void textRecordPosition_TextChanged(object sender, EventArgs e)
{
this.bindingSource.Position = Convert.ToInt32(textRecordPosition.Text) -1;
}
private void bindingSource_PositionChanged(object sender, EventArgs e)
{
this.controller.RecordPosition = this.bindingSource.Position + 1;
}
In my controller I have
public int RecordPosition
{
get
{
return this._position;
}
set
{
this._position = value;
this.SendChange("RecordPosition");
}
}
In my BindHelper class I have
public static void BindText(TextBox box, object dataSource, string dataMember)
{
var bind = new Binding("Text", dataSource, dataMember, true, DataSourceUpdateMode.OnPropertyChanged);
box.DataBindings.Add(bind);
}
I have an MVVM app with a data bound DataGrid bound to Resources<ResourceViewModel>. I add an additonal resource/row to the resource data shown in the DataGrid via
private void OnResourceAddedToCollection(object sender, ResourceCollectionChangedEventArgs e)
{
var viewModel = new ResourceViewModel(e.NewResource);
Resources.Add(viewModel);
}
This works. However, I take exactly the same approach and attempt to add columns to the DataGrid via
private void OnResourceCultureCollectionChanged(object sender,
ResourceCulturesCollectionChangedEventArgs e)
{
Resources.Clear();
foreach (var rvm in e.NewResourceCollection.Select(r => new ResourceViewModel(r)).ToList())
Resources.Add(rvm);
}
where the event arguments hold the updated underlying Resources. Now I have checked the data and it is right - the columns are being added, but the DataGrid is not showing the new columns. What am i doing wrong?
When adding columns you need to repaint the datagrid, reset your DataGrid.ItemSource should do it.
VIEW:
public SomeView(IViewModel vm)
{
ViewModel = vm;
DataContext = ViewModel;
InitializeComponent();
ViewModel.PropertyChanged += (s, e) =>
{
switch (e.PropertyName)
{
case "IsResetingColumns":
if (!ViewModel.IsResetingColumns)
{
dataGrid.ItemsSource = null;
dataGrid.ItemsSource = ViewModel.Resources;
}
break;
}
};
}
VIEWMODEL:
public class ViewModel : INotifyPropertyChanged
{
private bool _isResetingColumns;
public bool IsResetingColumns
{
get
{
return _isResetingColumns;
}
set
{
if (_isResetingColumns == value)
return;
_isResetingColumns = value;
OnPropertyChanged("IsResetingColumns");
}
}
private void OnResourceCultureCollectionChanged(object sender,
ResourceCulturesCollectionChangedEventArgs e)
{
//VIEWMODEL IS SETTING THIS TO TRUE BEFORE RESETTING `RESOURCES`
IsResetingColumns = true;
Resources.Clear();
foreach (var rvm in e.NewResourceCollection.Select(r => new ResourceViewModel(r)).ToList())
Resources.Add(rvm);
//VIEW'S LISTENING TO THIS PROPERTY CHANGED (naming could be better, or an event ,but the gist is there)
IsResetingColumns = false;
}
I'm attempting my first Windows Form project, having been entirely web based previously and experiencing some issues. I want to bind a list of objects to a TabControl and have this create the Tabs and then have a databound value accessible from the click event of each tab.
The Object I'm wanting to bind is
public class TreeNodeItem
{
private NTree<string> node;
public TreeNodeItem(NTree<string> node)
{
this.node = node;
}
public string Value
{
get { return this.node.data; }
}
}
The NTree node represents a node in an object that models data in a tree structure. I want to create a tab for each object in the list with the Value property being bound to the Tab Text property. Other posts mention binding to the ItemsSource property of the control, but Visual Studio is not giving me this property.
Any help will be greatly appreciated.
Cheers
Stewart
Okay, I was unaware of that the binding was a must. Although I have never seen something like this being done in a Windows Forms Application, I've decided to create a class that does this for us.
It uses the ObservableCollection<T> to keep track whether an object / property has been changed inside its list.
public class ObservableList<T> : ObservableCollection<T>
{
public ObservableList() : base()
{
CollectionChanged += new NotifyCollectionChangedEventHandler(nObservableCollection_CollectionChanged);
}
public event PropertyChangedEventHandler OnPropertyChanged;
void nObservableCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (OnPropertyChanged != null)
{
OnPropertyChanged(new object[] { e.OldItems, e.NewItems }, null); // Call method to let it change the tabpages
}
}
}
Now, we have to create a helper class that helps us keeping track:
public class TabControlBind
{
public TabControlBind(TabControl tabControl)
{
// Create a new TabPageCollection and bind it to our tabcontrol
this._tabPages = new TabControl.TabPageCollection(tabControl);
}
// Fields
private ObservableList<TreeNodeItem> _treeNodeItems;
private TabControl.TabPageCollection _tabPages;
// Properties
public ObservableList<TreeNodeItem> TreeNodeItems
{
get { return _treeNodeItems; }
set
{
if (_treeNodeItems != value)
{
_treeNodeItems = value;
_treeNodeItems.OnPropertyChanged += OnPropretyChanged;
OnPropretyChanged(null, null);
}
}
}
public TabControl.TabPageCollection TabPages
{
get
{
return this._tabPages;
}
}
// Events
private void OnPropretyChanged(object sender, PropertyChangedEventArgs e)
{
if (sender == null) // If list got set
{
// Remove existing tabpages
this._tabPages.Clear();
// Loop through all items inside the ObservableList object and add them to the Tabpage
foreach (TreeNodeItem _treeNodeItem in this._treeNodeItems)
{
TabPage tabPage = new TabPage() { Text = _treeNodeItem.Value, Tag = _treeNodeItems };
this._tabPages.Add(tabPage);
}
}
else if (sender is object[]) // If only one (or multiple) objects have been changed
{
// Get OldItems and NewItems
object[] changedItems = (object[])sender;
// Remove OldItems
if (changedItems[0] != null)
{
foreach (dynamic oldItems in (IList)changedItems[0])
{
foreach (TabPage tab in this._tabPages)
{
if (tab.Text == oldItems.Value)
{
this._tabPages.Remove(tab);
break;
}
}
}
}
// Add OldItems
if (changedItems[1] != null)
{
foreach (dynamic newItems in (IList)changedItems[1])
{
TabPage tabPage = new TabPage() { Text = newItems.Value, Tag = newItems };
this._tabPages.Add(tabPage);
}
}
}
}
}
This is a sample on how to use it:
TabControlBind tabControlBinder;
ObservableList<TreeNodeItem> treeNodeItems;
private void btnAdd_Click(object sender, EventArgs e)
{
// This will automatically update the TabControl
treeNodeItems.Add(new TreeNodeItem(new NTree<string>() { data = "Test3" }));
}
private void frmMain_Load(object sender, EventArgs e)
{
// Create a new list object an add items to it
treeNodeItems = new ObservableList<TreeNodeItem>();
treeNodeItems.Add(new TreeNodeItem(new NTree<string>() { data = "Test" }));
treeNodeItems.Add(new TreeNodeItem(new NTree<string>() { data = "Test2" }));
// Create a new instance of the TabControlBind class, set it to our TabControl
tabControlBinder = new TabControlBind(tabControl);
tabControlBinder.TreeNodeItems = treeNodeItems;
}