Background:
Include the TreeView control in the module's UI, with a check box defined in front of each node of the tree control.
Demand:
The control is used in two different applications, and its appearance in different applications is inconsistent, in this case, a display check box, one does not appear.
Problem:
One difficulty in solving this problem is that the design principle of the prism framework itself-this view is added to the region of the shell of the main program, so you cannot directly control the view's properties and its logic in the main program.
Ideas:
The use of Eventaggregator allows the main program to communicate with the module, thereby indirectly achieving our goal.
Realize:
First, add an attribute showcheckbox to the ViewModel of the module's View, as follows:
Public BOOL ShowCheckBox { getreturnthis. ShowCheckBox;} Set this. SetProperty (refthis. ShowCheckBox, value);} }
and add a binding to the View for it, following the binding of the Visibility property of the CheckBox:
<usercontrol.resources> <hierarchicaldatatemplatex:key= "Hierarchicalview"ItemsSource="{Binding subcategories}"> <StackPanelOrientation= "Horizontal"HorizontalAlignment= "Stretch"> <CheckBoxIsChecked="{Binding IsChecked}"Margin= "6,6,5,0" Visibility= "{Binding Showcheckbox,elementname=treeviewcontrol, Converter={staticresource Booltovisibilityconverter}}" /> <TextBlockText="{Binding Name}"FontSize= " the" /> <Stackpanel.tooltip> <TextBlockVerticalAlignment= "Center"Text="{Binding Name}"textwrapping= "Wrap"MaxWidth= "$" /> </Stackpanel.tooltip> </StackPanel> </hierarchicaldatatemplate></usercontrol.resources>
Then we need to define an event in the underlying library Infrastructure, where the main program and module communicate through the event to modify the UI values in the module as follows:
Public class controlvisibleevent:pubsubevent<bool> { }
Next, the event is subscribed and published, naturally, we subscribe to the above event in the module, the code is as follows:
this. Eventaggregator.getevent<demobase.controlvisibleevent> (). Subscribe ((value) = = { this. ShowCheckBox = value; });
When subscribing to an event, we get the value and pass the obtained value to the ShowCheckBox property of ViewModel, and then through the notification mechanism of WPF to achieve UI changes.
In the main program, we publish this event, the issue to be considered when publishing, we choose to publish in the event of loading module completion, the code is as follows (note the highlighted code):
PublicMainWindow (Ieventaggregator eventaggregator, Imodulemanager Modulemanager) {InitializeComponent (); This. Eventaggregator =Eventaggregator; This. modulemanager.loadmodulecompleted + =modulemanager_loadmodulecompleted; } voidModulemanager_loadmodulecompleted (Objectsender, Loadmodulecompletedeventargs e) { This . Eventaggregator.getevent<demobase.controlvisibleevent> (). Publish (false ); }
At this point, in different applications, you can change the UI in the module as long as you modify the value at the time the event was published.
Note:
This requires more than the above method, in addition, the use of the Regioncontext attribute of the region object is also a good way, follow-up can be further studied.
In the PRISM framework, the communication between the main program and the module UI is realized.