First of all, there are a number of different types of UserControl used to display data in different ways, in order to display the data in the view, only the logically associated UserControl is displayed (Visiable), by implementing some sort of click-Check button in another view. All other UserControl are folded (collasped) up. This approach is similar to how several controls are bound to a set of Radiobuttongroup, and which RadioButton is selected to show which part of the content.
Although I know there is a way to modify the template, I should be able to do it, but I have not studied this part. So I thought of the individual strokes.
Because it is on a different view, it cannot simply be implemented in a way that binds to the isselected->visilibity of the element.
Then you can implement a function similar to Radiobuttongroup.
1. First prepare a class to bind to the Visilibity property of my UserControl.
Public class isselectedclass{ protectedbool isSelected; Public BOOL
{
Get = =isSelected ; Set { = value; OnPropertyChanged (nameof (IsSelected)); } } }
2. Prepare a grouplist to store the Isselectedcalss object we added (add)
New List<isselectedcalss> ();
3. For better extensibility and compatibility, ISSELECTEDCALSS and list<isselectedcalss> are then repackaged in generic form
Also, because the IsSelected property is bound to the visibility property of UserControl, the INotifyPropertyChanged interface is implemented.
We treat the Radiogrouplist class as a Radiobuttongroup class, adding objects that inherit from the Isselectedclass class to the Radiogrouplist object.
And when the Isselectedclass class object is added to the list, subscribe to its PropertyChanged event so that we can receive a notification when the object's IsSelected property value changes. The other Isselectedclass objects in the list are then processed.
Remember to unsubscribe from this event when the object is removed. Even though this has little impact on performance, and remove is not commonly used, a good planning for memory is a basic accomplishment for programmers.
1 Public classisselectedclass:inotifypropertychanged2 {3 protected BOOLisSelected;4 Public Virtual BOOLIsSelected5 {6 Get=isSelected;7 Set8 {9isSelected =value;Ten onpropertychanged (nameof (IsSelected)); One } A } - Public EventPropertyChangedEventHandler propertychanged; - the [Notifypropertychangedinvocator] - protected Virtual voidOnPropertyChanged ([Callermembername]stringPropertyName =NULL) - { -PropertyChanged?. Invoke ( This,NewPropertyChangedEventArgs (PropertyName)); + } - + Public voidsetselectedtrue () A { at This. isSelected =true; - } - } - - Public classRadiogrouplist<t>whereT:isselectedclass - { inlist<t> list =NewList<t>(); - to Public voidaddradioselector (T t) + { - list. ADD (t); theT.propertychanged + =Tselectedpropertychangedeventhandler; * $ }Panax Notoginseng - Private voidTselectedpropertychangedeventhandler (Objectsender, PropertyChangedEventArgs e) the { + vart = Sender asIsselectedclass; A if(E.propertyname = =nameof (t.isselected)) the { + if(t.isselected) - { $ for(inti =0; I < list. Count; i++) $ { -List[i]. IsSelected =false; - } the t.setselectedtrue (); - }Wuyi } the } - Wu Public voidremoveselector (T t) - { About if(list. Contains (t)) $ { - list. Remove (t); -T.propertychanged-=Tselectedpropertychangedeventhandler; - } A } +}
Key Code
4. To improve the use of the Radiogrouplist<t> class, add several common methods and indexers similar to list.
Public voidaddrangeselector (t[] T) {foreach(varVARIABLEincht) {addradioselector (VARIABLE); } } Public voidClear () {foreach(varVARIABLEinchlist) {VARIABLE. PropertyChanged-=Tselectedpropertychangedeventhandler; } list. Clear (); } PublicT This[intIndex] = List[index];
5. Simple test performance
Because I iterate over the elements in the list two times, I have generated 200 objects to test their performance and see if this is going to kill the interface.
PublicShellviewmodel ()//constructor, nothing to say.{radiogrouplist<IsSelectedClass> islist =NewRadiogrouplist<isselectedclass>(); intTcount = $;//controlling the number of loop-generated objects for(inti =0; i < Tcount; i++) {islist. Addradioselector (NewIsselectedclass ()); } islist[ the]. IsSelected =true; islist[111]. IsSelected =true; islist[138]. IsSelected =true; islist[198]. IsSelected =true; for(inti =0; i < Tcount; i++) {Console.WriteLine ($"{I}={islist[i]. IsSelected}"); } }
The final output is that only the value of index 198 is true, and all others are false. And the program executes very quickly, which is displayed almost when it is started.
No timer test, because this is for the control, my control is not more than 10, so the performance is sufficient.
Although this function is very simple, but I as a small rookie, still quite happy. Welcome to the message exchange ~
radiogrouplist<T>
C # uses list to implement a Radiobuttongroup-like radio feature