2323using System . Windows . Forms ;
2424using System . ComponentModel ;
2525using MethodInvoker = System . Windows . Forms . MethodInvoker ;
26+ using RibbonLib . Controls ;
2627
2728namespace RibbonLib
2829{
@@ -36,8 +37,7 @@ public class Ribbon : Control, IUICommandHandler
3637 private IUIImageFromBitmap _imageFromBitmap ;
3738 private UIImage _uiImage ;
3839 private RibbonUIApplication _application ;
39- private Dictionary < uint , IRibbonControl > _mapRibbonControls = new Dictionary < uint , IRibbonControl > ( ) ;
40- internal Dictionary < uint , IRibbonControl > MapRibbonControls { get { return _mapRibbonControls ; } }
40+ private Dictionary < uint , BaseRibbonControl > _mapRibbonControls = new Dictionary < uint , BaseRibbonControl > ( ) ;
4141 private IntPtr _loadedDllHandle = IntPtr . Zero ;
4242
4343 private const string DefaultResourceName = "APPLICATION_RIBBON" ;
@@ -1122,7 +1122,7 @@ private static IUIImageFromBitmap CreateImageFromBitmapFactory()
11221122 /// Adds a ribbon control to the internal map
11231123 /// </summary>
11241124 /// <param name="ribbonControl">ribbon control to add</param>
1125- internal void AddRibbonControl ( IRibbonControl ribbonControl )
1125+ internal void AddRibbonControl ( BaseRibbonControl ribbonControl )
11261126 {
11271127 _mapRibbonControls . Add ( ribbonControl . CommandID , ribbonControl ) ;
11281128 }
@@ -1172,9 +1172,10 @@ HRESULT IUICommandHandler.Execute(uint commandID, ExecutionVerb verb, PropertyKe
11721172 Debug . WriteLine ( string . Format ( "Execute verb: {0} for command {1}" , verb , commandID ) ) ;
11731173#endif
11741174
1175- if ( _mapRibbonControls . ContainsKey ( commandID ) )
1175+ if ( TryGetRibbonControlById ( commandID , out BaseRibbonControl control ) )
11761176 {
1177- return _mapRibbonControls [ commandID ] . Execute ( verb , key , currentValue , commandExecutionProperties ) ;
1177+ IRibbonControl item = control as IRibbonControl ;
1178+ return item . Execute ( verb , key , currentValue , commandExecutionProperties ) ;
11781179 }
11791180
11801181 return HRESULT . S_OK ;
@@ -1196,9 +1197,10 @@ HRESULT IUICommandHandler.UpdateProperty(uint commandID, ref PropertyKey key, Pr
11961197 Debug . WriteLine ( string . Format ( "UpdateProperty key: {0} for command {1}" , RibbonProperties . GetPropertyKeyName ( ref key ) , commandID ) ) ;
11971198#endif
11981199
1199- if ( _mapRibbonControls . ContainsKey ( commandID ) )
1200+ if ( TryGetRibbonControlById ( commandID , out BaseRibbonControl control ) )
12001201 {
1201- return _mapRibbonControls [ commandID ] . UpdateProperty ( ref key , currentValue , ref newValue ) ;
1202+ IRibbonControl item = control as IRibbonControl ;
1203+ return item . UpdateProperty ( ref key , currentValue , ref newValue ) ;
12021204 }
12031205
12041206 return HRESULT . S_OK ;
@@ -1285,5 +1287,25 @@ public IntPtr MarkupHandle
12851287 return _loadedDllHandle ;
12861288 }
12871289 }
1290+
1291+ /// <summary>
1292+ /// Get the control by commandId
1293+ /// </summary>
1294+ /// <param name="commandId"></param>
1295+ /// <returns>IRibbonControl</returns>
1296+ /// <exception cref="ArgumentException"></exception>
1297+ public IRibbonControl GetRibbonControlById ( uint commandId )
1298+ {
1299+ bool result = _mapRibbonControls . TryGetValue ( commandId , out BaseRibbonControl item ) ;
1300+ if ( result )
1301+ return item ;
1302+ throw new ArgumentException ( "Not found" , nameof ( commandId ) ) ;
1303+ }
1304+
1305+ internal bool TryGetRibbonControlById ( uint commandId , out BaseRibbonControl item )
1306+ {
1307+ bool result = _mapRibbonControls . TryGetValue ( commandId , out item ) ;
1308+ return result ;
1309+ }
12881310 }
12891311}
0 commit comments