I've defined an Observable collection as shown below,
public class PropertyFieldsInExcel
{
public string LongNames { get; set; }
public string ShortNames { get; set; }
public string CNames { get; set; }
}
static ObservableCollection<PropertyFieldsInExcel> Properties =
new ObservableCollection<PropertyFieldsInExcel>();
I have a method which changes the the value of some of the elements in that class like so,
public static void AutofillCell()
{
((INotifyPropertyChanged)Properties).PropertyChanged +=
new PropertyChangedEventHandler(PropertyChangedEvent);
Properties[i].CNames = "It works";
Properties[i].CNames = "Ha ha ha";
((INotifyPropertyChanged)Properties).PropertyChanged -=
new PropertyChangedEventHandler(PropertyChangedEvent);
}
When I assign a value to a particular element as shown above, the event does not fire. Why? What is the mistake I've committed?
The code of the event handler is like so,
private static void PropertyChangedEvent(object sender, PropertyChangedEventArgs e)
{
//Some code to be executed
}
Two problems:
1) PropertyFieldsInExcel does not implement INotifyPropertyChanged
2) ObservableCollection can inform you when items are changing, but only after you manually subcribe to the changed event of all items.
The link in the comment from Uwe Keim gives an exellent explanation with examples...
Related
I'm fairly new to C# and I'm trying to create a Hangman game in WinForms, I've got the game functionality working, but I'm trying to create a form where the user selects a category and then the word to guess is from the category selected.
I've got a HangEventArgs like below:
public class HangEventArgs : EventArgs
{
public Category WordCategory { get; set; }
}
and a class for the data (I'm hoping to expand it to add more features in the future).
public enum Category
{
// Categories are stores here
}
public class HangData
{
public Category WordCategory { get; protected set; }
public HangData(Category askWhat)
{
WordCategory = askWhat;
}
}
And a class where the words are stored
public static class WordsToGuess
{
public static string[] Capitals =
{
"London",
"Paris" // more words here
}; // more categories here
Finally I have my button click event for all the categories, I've created my own Button as to not use the default EventArgs.
private void bCategory_Click(object sender, HangEventArgs e)
{
MainGame mg = new MainGame(new HangData(e.WordCategory));
mg.ShowDialog();
}
I've been trying to use event handlers like so
public event EventHandler<HangEventArgs>(object sender, HangEventArgs e);
But I'm not sure the proper way to implement this into my code.
If I use
bCapitals.Click += new EventHandler(bCategory_Click);
I get a no overload matches delegate error and I'm stuck on how to fix it. Thanks for the help in advance.
Create your category button like this:
public class CategoryButton : Button
{
protected override void OnClick(EventArgs e)
{
// Just discard the `e` argument and pass your own argument.
base.OnClick(new HangEventArgs { WordCategory = Category.Cities });
}
}
Subscribe the event with:
categoryButton1.Click += CategoryButton1_Click;
Use like this
private void CategoryButton1_Click(object sender, EventArgs e)
{
if (e is HangEventArgs hangEventArgs) {
MessageBox.Show(hangEventArgs.WordCategory.ToString());
}
}
Note that the click mechanism still works as expected. You don't need to fire the event yourself.
Of course you could create your own event; however, then, it must have a different name like HangClick and you must fire it yourself.
public class CategoryButton : Button
{
public event EventHandler<HangEventArgs> HangClick;
protected virtual void OnHangClick(HangEventArgs e)
{
HangClick?.Invoke(this, e);
}
protected override void OnClick(EventArgs e)
{
OnHangClick(new HangEventArgs { WordCategory = Category.Cities });
// Optionally, if you want to preserve the standard click event behaviour:
base.OnClick(e);
}
}
Subscribe with:
categoryButton1.HangClick += CategoryButton1_HangClick;
Use like this:
private void CategoryButton1_HangClick(object sender, HangEventArgs e)
{
MessageBox.Show(e.WordCategory.ToString());
}
Let's suppose I have an observable collection and two clients that want to:
change it,
observe it and react on state change.
Now, if Client1 changes collection state (for example: adds new item), the collection will fire 'CollectionChanged' event. Since both clients are registered for this event notifications, Client1's handling method will be executed.
In order to avoid self-callback on Client1, I unsubscribe from an event, do my action and subscribe again. This is painful - I must remember about suspending Client1's subscription every time Client1 touches the collection and it just seems like a bad smell. Is there a better way (design pattern, external library) that would help me in callbacks management?
Although in my example I mentioned ObservableCollection and CollectionChanged event, I believe my question is more generic and comes down to: "how to exclude an entity that caused event trigger from event callback".
Thanks in advance!
Problem keeps reoccuring in my solution, bumping the question in a hope someone might help out.
I ran into your problem some times ago I didn't find a proper solution except for this one.
The idea is that when you change the collection you also pass an instance of the object changing it.
Then when the Collection fires the event, it also passes the reference.
So all observers may know which instance did the change, and check for equality.
Here is a basic example of this implementation:
class Program
{
private static MyCollection Collection;
private static MyCollectionModifier Modif1;
private static MyCollectionModifier Modif2;
static void Main(string[] args)
{
Collection = new MyCollection();
Modif1 = new MyCollectionModifier("Modifier 1", Collection);
Modif2 = new MyCollectionModifier("Modifier 2", Collection);
Modif1.AddItem("Test1");
Modif2.AddItem("Test2");
Console.ReadKey();
}
}
public class MyCollectionItemAddedEventArgs:EventArgs
{
public Object ChangeSource { get; set;}
public int newIndex {get;set;}
}
public delegate void MyCollectionItemAddedEventHandler(object sender, MyCollectionItemAddedEventArgs e);
public class MyCollection
{
private List<String> _myList;
public String this[int Index]
{
get { return _myList[Index]; }
}
public event MyCollectionItemAddedEventHandler ItemAdded;
public MyCollection()
{
_myList = new List<string>();
}
protected virtual void OnMyCollectionItemAdded(MyCollectionItemAddedEventArgs e)
{
if (ItemAdded != null)
ItemAdded(this, e);
}
public void AddItem(String Item, object ChangeSource = null)
{
_myList.Add(Item);
var e = new MyCollectionItemAddedEventArgs();
e.ChangeSource = ChangeSource;
e.newIndex = _myList.Count;
OnMyCollectionItemAdded(e);
}
}
public class MyCollectionModifier
{
private MyCollection _collection;
public string Name { get; set; }
public MyCollectionModifier(string Name, MyCollection Collection)
{
this.Name = Name;
_collection = Collection;
_collection.ItemAdded += Collection_ItemAdded;
}
public void AddItem(string Item)
{
_collection.AddItem(Item, this);
}
void Collection_ItemAdded(object sender, MyCollectionItemAddedEventArgs e)
{
if (e != null)
{
if (this.Equals(e.ChangeSource))
{
Console.WriteLine("{0} : I changed the collection", Name);
}
else
{
Console.WriteLine("{0} : Somebody else changed the collection", Name);
}
}
}
}
I've encountered this problem before as well.
Best solution I could come up with is to create extension methods that take the handler of the caller and then automate the unsubscribe/subscribe around the called method, that way you don't have to remember to do it each time and it does not end up cluttering your code either
public static void Add<T>(this ObservableCollection<T> self, T itemToAdd, NotifyCollectionChangedEventHandler handler)
{
self.CollectionChanged -= handler;
self.Add(itemToAdd);
self.CollectionChanged += handler;
}
It does take some effort to create the extensions initially but at least you won't forget to resubscribe. Only real extra code is then around invoking the method
public class ObserverClass
{
public ObserverClass()
{
ObservableIntegers.CollectionChanged += ObservableIntegersOnCollectionChanged;
//Add item to collection while preventing self-handling the callback
ObservableIntegers.Add(1, ObservableIntegersOnCollectionChanged);
}
private void ObservableIntegersOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
{
// Handle collection change
}
public ObservableCollection<int> ObservableIntegers { get; set; }
}
I am writing windows phone 8.1 universal application and main applicaiton control is Pivot with few pivot items. In the pivot items are ListViews containing TestItems. I want to filter items on one list by IsRead property. Is it possible to just filter main collection without keeping 2 collections? CollectionViewSource does not support filtering a sorting on universal apps, if I know. But keeping (and synchronizing on changes) two collections doesn't look like good idea.
EDIT:
I have used ObservableCollection because list of items may be updated on the background. Probably it was not clear from original question.
class TestItem : ModelBase
{
private bool isRead;
public bool IsRead
{
get { return isRead; }
set
{
isRead = value;
NotifyPropertyChanged();
}
}
private string name;
public string Name
{
get { return name; }
set
{
name = value;
NotifyPropertyChanged();
}
}
}
class MainViewModel : INotifyPropertyChanged
{
public MainViewModel()
{
Items = new ObservableCollection<TestItem>();
}
public ObservableCollection<TestItem> Items { get; private set; }
public ObservableCollection<TestItem> ItemsRead { get; private set; } // key point
private void RefreshItems()
{
// data manipulation - on both collections?
}
// ...
}
You can use Linq;
In your case:
using System.Linq;
class MainViewModel : INotifyPropertyChanged
{
public MainViewModel()
{
Items = new ObservableCollection<TestItem>();
}
public ObservableCollection<TestItem> Items { get; private set; }
//public ObservableCollection<TestItem> ItemsRead { get; private set; } // key point
public IEnumerable<TestItem> ItemsRead
{
get
{
IEnumerable<TestItem> itemsRead = from item in Items
where item.IsRead
select item;
return itemsRead;
}
}
private void RefreshItems()
{
// data manipulation - on both collections?
}
// ...
}
Please, check syntax, it can contain some mistakes.
You can manipulate with the first collection, the second collection will be automatically updated.
You can define a CollectionViewSource in your XAML:
<Grid.Resources>
<CollectionViewSource x:Name="MyCollectionViewSource"/>
</Grid.Resources>
And then set it's source like this:
//Global variable
MainViewModel vm;
//Constructor
public MyPage(){
//Other code
vm = new MainViewModel();
vm.Items.CollectionChanged += Items_CollectionChanged;
UpdateViewSource();
}
private void Items_CollectionChanged(object sender, CollectionChangedEventArgs e){
UpdateViewSource();
}
private void UpdateViewSource(){
MyCollectionViewSource.Source = vm.Items.Where(x => x.IsRead);
}
I haven't tested this code.
You need only one ObservableCollection containing the initial objects and another property (let's say ItemsFiltered) with a get method returning the results after filtering. In the constructor you can subscribe to the CollectionChanged event of the observable collection to raise the OnPropertyChanged event for the ItemsFiltered property. You raise the same event when the filter state is changed. This is a simple example:
public MainViewModel()
{
_initialItems.CollectionChanged += (sender, e) => OnPropertyChanged("Items");
}
private ObservableCollection<TestItem> _initialItems = new ObservableCollection<TestItem>();
public List<TestItem> Items
{
get
{
if (IsReadFilter)
{
return _initialItems.Where(i => i.IsRead).ToList();
}
return _initialItems;
}
}
private bool _isReadFilter;
public bool IsReadFilter
{
get { return _isReadFilter; }
set
{
if (_isReadFilter != value)
{
_isReadFilter = value;
OnPropertyChanged("IsReadFilter");
OnPropertyChanged("Items");
}
}
}
Basically, the idea is that every time IsReadFilter value is changed, the UI gets notified that the Items property is changed and calls its get method to get the new value and update. Items are also updated every time the observable collection is changed from other places.
I want to bind a custom property of a windows form to a second property, so when I update the former the latter gets the same value.
This is the simplest example of what I'm trying to do:
public partial class Form2 : Form
{
public string MyTargetProperty { get; set; }
public string OtherProperty { get; set; }
public Form2()
{
InitializeComponent();
this.DataBindings.Add("MyTargetProperty", this, "OtherProperty");
}
private void button1_Click(object sender, EventArgs e)
{
MyTargetProperty = "test";
Console.WriteLine("OtherProperty " + OtherProperty);
}
}
When I click button1 I should be able to see that 'OtherProperty' has the same value as 'MyTargetProperty'. Am I doing something wrong? Do I miss something?
Your form needs to implement INotifyPropertyChanged for the MyTargetProperty.
Example:
class FooForm : Form, INotifyPropertyChanged
{
private int myTargetProperty;
public int MyTargetProperty
{
get { return this.myTargetProperty; }
set
{
this.myTargetProperty = value;
this.OnPropertyChanged(
new PropertyChangedEventArgs("MyTargetProperty"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(PropertyChangedEventArgs e)
{
var evt = this.PropertyChanged;
if (evt != null)
evt(this, e);
}
}
Then you need to add the binding like this:
this.DataBindings.Add(
"OtherProperty",
this,
"MyTargetProperty",
false,
DataSourceUpdateMode.Never);
This will perform a one way binding. The MyTargetProperty will never be updated when the OtherProperty changes. If you need a two way binding you change the DataSourceUpdateMode and also implement a PropertyChanged for OtherProperty.
I'm trying to Databind to my custom dictionary class. In formLoad, I can bind to Car.Desc but I cannot bind to RatesCache.Desc.
They are both public string properties.
What am I missing?
Thanks!
System.ArgumentException was unhandled
Message="Cannot bind to the property or column Desc on the DataSource.\r\nParameter name: dataMember"
Source="System.Windows.Forms"
ParamName="dataMember"
public class RatesCache : Dictionary<int, Rate>
{
public string Desc { get; set; }
}
public class Car
{
public string Desc { get; set; }
}
static Car car = new Car();
static RatesCache rc = new RatesCache();
private void Form1_Load(object sender, EventArgs e)
{
rc.Desc = "hello too";
car.Desc = "Im a car";
textBox1.DataBindings.Add("Text", rc, "Desc");
}
private void Form1_Load(object sender, EventArgs e)
{
rc.Desc = "hello too";
car.Desc = "Im a car";
textBox1.DataBindings.Add("Text", rc, "Desc");
textBox1.TextChanged .TextChanged += _textBox1_TextChanged;
}
private void _messagesReceviedLabel_TextChanged(object sender, EventArgs e)
{
_textBox1.Text = rc.Desc.ToString();
}
public class RatesCache : Dictionary<int, Rate>
{
public string Desc { get; set; }
public override string ToString()
{
return Desc;
}
}
My guess is that because your class is inheriting from a Dictionary which is a Collection, it throws off the DataBinding for the textbox. Windows Forms has it's own way of dealing with databinding to a collection different then when binding directly to a property of a class. Not much of an answer, I know, but I don't think there's really a way around it. My suggestion would be to either not directly inherit from Dictionary; rather keep an internal Dictionary, and expose methods as needed. OR, don't databind the texbox directly. Rather, raise an event whenever your "Desc" property changes in your RatesCache class, and then in your form listen to that event. When it changes, update your textbox.