Skip to content
Browse files

Combobox with Commands

  • Loading branch information...
1 parent 6cf37cd commit 7bc2c5db2d6f2671f71e0ab86e7413ef81b1d399 @PeterForstmeier PeterForstmeier committed Sep 21, 2011
View
7 src/AddIns/Analysis/CodeQuality/CodeQualityAnalysis.csproj
@@ -142,11 +142,12 @@
<Compile Include="Src\Relationship.cs" />
<Compile Include="Src\RelationshipType.cs" />
<Compile Include="Src\Type.cs" />
+ <Compile Include="Src\Utility\ComboBoxWithCommand.cs" />
<Compile Include="Src\Utility\DoubleKeyDictionary.cs" />
<Compile Include="Src\Utility\IColorizer.cs" />
<Compile Include="Src\Utility\Helper.cs" />
- <Compile Include="Src\Utility\LocalizeableCombo\EnumToFriendlyNameConverter.cs" />
- <Compile Include="Src\Utility\LocalizeableCombo\LocalizableDescriptionAttribute.cs" />
+ <Compile Include="Src\Utility\Localizeable\EnumToFriendlyNameConverter.cs" />
+ <Compile Include="Src\Utility\Localizeable\LocalizableDescriptionAttribute.cs" />
<Compile Include="Src\Utility\Matrix.cs" />
<Compile Include="Src\Utility\RelayCommand.cs" />
<Compile Include="Src\Utility\ViewModelBase.cs" />
@@ -270,7 +271,7 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Src\Utility" />
- <Folder Include="Src\Utility\LocalizeableCombo" />
+ <Folder Include="Src\Utility\Localizeable" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
View
27 src/AddIns/Analysis/CodeQuality/Src/MainWindow.xaml
@@ -12,7 +12,8 @@
xmlns:tree="http://icsharpcode.net/sharpdevelop/treeview"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:t="clr-namespace:TreeMaps.Controls;assembly=TreeMaps"
- xmlns:loc ="clr-namespace:ICSharpCode.CodeQualityAnalysis.Utility.LocalizeableCombo"
+ xmlns:loc ="clr-namespace:ICSharpCode.CodeQualityAnalysis.Utility.Localizeable"
+ xmlns:util="clr-namespace:ICSharpCode.CodeQualityAnalysis.Utility"
Title="{Binding FrmTitle}"
x:Name="root">
@@ -279,16 +280,18 @@
Grid.Column="0">
<ToolBar>
<TextBlock Text="Level: " />
- <!-- SelectedItem="{Binding MetricsLevelSelectedItem}"-->
- <ComboBox Width="200"
- ItemsSource="{Binding Source={StaticResource MetricsLevel}}"></ComboBox>
+
+ <util:ComboBoxWithCommand Width="200"
+ ItemsSource="{Binding Source={StaticResource MetricsLevel}}"
+ Command="{Binding ActivateMetrics}">
+ </util:ComboBoxWithCommand>
<TextBlock Margin="4,0,0,0" Text="Metric: " />
-
- <ComboBox Width="200" Margin="4,0,0,0"
- ItemsSource="{Binding Source={StaticResource Metrics}}"
- SelectedItem="{Binding SelectedMetrics}">
- <ComboBox.ItemTemplate>
+
+ <util:ComboBoxWithCommand Width="200" Margin="4,0,0,0"
+ ItemsSource="{Binding Source={StaticResource Metrics}}"
+ Command="{Binding ShowTreeMap}">
+ <util:ComboBoxWithCommand.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Path=.,Mode=OneWay,
Converter={StaticResource enumItemsConverter}}"
@@ -297,10 +300,10 @@
VerticalAlignment="Center"/>
</DataTemplate>
- </ComboBox.ItemTemplate>
- </ComboBox>
+ </util:ComboBoxWithCommand.ItemTemplate>
+ </util:ComboBoxWithCommand>
+
</ToolBar>
-
</ToolBarTray>
<!-- http://treemaps.codeplex.com/-->
View
57 src/AddIns/Analysis/CodeQuality/Src/MainWindowModel.cs
@@ -16,7 +16,7 @@
using ICSharpCode.CodeQualityAnalysis.Controls;
using ICSharpCode.CodeQualityAnalysis.Utility;
-using ICSharpCode.CodeQualityAnalysis.Utility.LocalizeableCombo;
+using ICSharpCode.CodeQualityAnalysis.Utility.Localizeable;
using Microsoft.Win32;
namespace ICSharpCode.CodeQualityAnalysis
@@ -50,16 +50,19 @@ public class MainWindowViewModel :ViewModelBase
public MainWindowViewModel():base()
{
- this.FrmTitle = "$Code Quality Analysis";
- this.btnOpenAssembly = "$Open Assembly";
+ this.FrmTitle = "Code Quality Analysis";
+ this.btnOpenAssembly = "Open Assembly";
#region MainTab
- this.TabDependencyGraph = "$Dependency Graph";
- this.TabDependencyMatrix = "$Dependency Matrix";
- this.TabMetrics = "$Metrics";
+ this.TabDependencyGraph = "Dependency Graph";
+ this.TabDependencyMatrix = "Dependency Matrix";
+ this.TabMetrics = "Metrics";
#endregion
MetrixTabEnable = false;
+
+ ActivateMetrics = new RelayCommand(ActivateMetricsExecute);
+ ShowTreeMap = new RelayCommand(ShowTreemapExecute,CanActivateTreemap);
}
@@ -147,10 +150,6 @@ public MainWindowViewModel():base()
base.RaisePropertyChanged(() =>this.Nodes);}
}
-//http://stackoverflow.com/questions/58743/databinding-an-enum-property-to-a-combobox-in-wpf#62032 http://stackoverflow.com/questions/58743/databinding-an-enum-property-to-a-combobox-in-wpf#62032
-
- //http://www.ageektrapped.com/blog/the-missing-net-7-displaying-enums-in-wpf/
-// http://www.codeproject.com/KB/WPF/FriendlyEnums.aspx
private string treeValueProperty ;
@@ -160,22 +159,27 @@ public MainWindowViewModel():base()
base.RaisePropertyChanged(() =>this.TreeValueProperty);}
}
- // First Combo
+ // MetricsLevel Combo
public MetricsLevel MetricsLevel {
get {return MetricsLevel;}
}
-// MetricsLevel metricsLevelSelectedItem = MetricsLevel.Assembly;
-//
-// public MetricsLevel MetricsLevelSelectedItem {
-// get { return metricsLevelSelectedItem; }
-// set { metricsLevelSelectedItem = value;
-// base.RaisePropertyChanged(() =>this.MetricsLevelSelectedItem);
-// }
-// }
+ #region ActivateMetrics
+
+ public ICommand ActivateMetrics {get;private set;}
+
+ bool metricsIsActive;
+
+ void ActivateMetricsExecute ()
+ {
+ metricsIsActive = true;
+ }
+
+ #endregion
+
- // Second Combo
+ // Metrics Combo
public Metrics Metrics
{
@@ -188,12 +192,19 @@ public Metrics Metrics
get { return selectedMetrics; }
set { selectedMetrics = value;
base.RaisePropertyChanged(() =>this.SelectedMetrics);
- ActivateTreemap();
}
}
+ #region ShowTreeMap Treemap
+
+ public ICommand ShowTreeMap {get;private set;}
+
+ bool CanActivateTreemap()
+ {
+ return metricsIsActive;
+ }
- void ActivateTreemap()
+ void ShowTreemapExecute()
{
var r = from ns in MainModule.Namespaces
from type in ns.Types
@@ -217,5 +228,7 @@ void ActivateTreemap()
throw new Exception("Invalid value for Metrics");
}
}
+
+ #endregion
}
}
View
164 src/AddIns/Analysis/CodeQuality/Src/Utility/ComboBoxWithCommand.cs
@@ -0,0 +1,164 @@
+/*
+ * Created by SharpDevelop.
+ * User: Peter Forstmeier
+ * Date: 21.09.2011
+ * Time: 19:23
+ *
+ * To change this template use Tools | Options | Coding | Edit Standard Headers.
+ */
+using System;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Input;
+
+namespace ICSharpCode.CodeQualityAnalysis.Utility
+{
+ /// <summary>
+ /// Description of ComboBoxWithCommand.
+ /// http://stackoverflow.com/questions/2222430/wpf-command-support-in-combobox
+ /// </summary>
+ public class ComboBoxWithCommand : ComboBox, ICommandSource
+ {
+ private static EventHandler canExecuteChangedHandler;
+
+ public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command",
+ typeof(ICommand),
+ typeof(ComboBoxWithCommand),
+ new PropertyMetadata((ICommand)null,
+ new PropertyChangedCallback(CommandChanged)));
+
+ public ICommand Command
+ {
+ get
+ {
+ return (ICommand)GetValue(CommandProperty);
+ }
+ set
+ {
+ SetValue(CommandProperty, value);
+ }
+
+ }
+
+ public static readonly DependencyProperty CommandTargetProperty = DependencyProperty.Register("CommandTarget",
+ typeof(IInputElement),
+ typeof(ComboBoxWithCommand),
+ new PropertyMetadata((IInputElement)null));
+
+ public IInputElement CommandTarget
+ {
+ get
+ {
+ return (IInputElement)GetValue(CommandTargetProperty);
+ }
+ set
+ {
+ SetValue(CommandTargetProperty, value);
+ }
+ }
+
+ public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter",
+ typeof(object),
+ typeof(ComboBoxWithCommand),
+ new PropertyMetadata((object)null));
+
+ public object CommandParameter
+ {
+ get
+ {
+ return (object)GetValue(CommandParameterProperty);
+ }
+ set
+ {
+ SetValue(CommandParameterProperty, value);
+ }
+ }
+
+ public ComboBoxWithCommand() : base() { }
+
+
+ private static void CommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
+ {
+ ComboBoxWithCommand cb = (ComboBoxWithCommand)d;
+ cb.HookUpCommand((ICommand)e.OldValue, (ICommand)e.NewValue);
+ }
+
+ private void HookUpCommand(ICommand oldCommand, ICommand newCommand)
+ {
+ if (oldCommand != null)
+ {
+ RemoveCommand(oldCommand, newCommand);
+ }
+ AddCommand(oldCommand, newCommand);
+ }
+
+ private void RemoveCommand(ICommand oldCommand, ICommand newCommand)
+ {
+ EventHandler handler = CanExecuteChanged;
+ oldCommand.CanExecuteChanged -= handler;
+ }
+
+ private void AddCommand(ICommand oldCommand, ICommand newCommand)
+ {
+ EventHandler handler = new EventHandler(CanExecuteChanged);
+ canExecuteChangedHandler = handler;
+ if (newCommand != null)
+ {
+ newCommand.CanExecuteChanged += canExecuteChangedHandler;
+ }
+ }
+ private void CanExecuteChanged(object sender, EventArgs e)
+ {
+
+ if (this.Command != null)
+ {
+ RoutedCommand command = this.Command as RoutedCommand;
+
+ // If a RoutedCommand.
+ if (command != null)
+ {
+ if (command.CanExecute(this.CommandParameter, this.CommandTarget))
+ {
+ this.IsEnabled = true;
+ }
+ else
+ {
+ this.IsEnabled = false;
+ }
+ }
+ // If a not RoutedCommand.
+ else
+ {
+ if (Command.CanExecute(CommandParameter))
+ {
+ this.IsEnabled = true;
+ }
+ else
+ {
+ this.IsEnabled = false;
+ }
+ }
+ }
+ }
+
+ protected override void OnSelectionChanged(SelectionChangedEventArgs e)
+ {
+ base.OnSelectionChanged(e);
+
+ if (this.Command != null)
+ {
+ RoutedCommand command = this.Command as RoutedCommand;
+
+ if (command != null)
+ {
+ command.Execute(this.CommandParameter, this.CommandTarget);
+ }
+ else
+ {
+ ((ICommand)Command).Execute(CommandParameter);
+ }
+ }
+ }
+ }
+
+}
View
2 ...eableCombo/EnumToFriendlyNameConverter.cs → ...calizeable/EnumToFriendlyNameConverter.cs
@@ -12,7 +12,7 @@
using System.Reflection;
using System.Runtime.Serialization;
-namespace ICSharpCode.CodeQualityAnalysis.Utility.LocalizeableCombo
+namespace ICSharpCode.CodeQualityAnalysis.Utility.Localizeable
{
/// <summary>
/// Description of EnumToFriendlyNameConverter.
View
2 ...eCombo/LocalizableDescriptionAttribute.cs → ...zeable/LocalizableDescriptionAttribute.cs
@@ -9,7 +9,7 @@
using System;
using System.ComponentModel;
-namespace ICSharpCode.CodeQualityAnalysis.Utility.LocalizeableCombo
+namespace ICSharpCode.CodeQualityAnalysis.Utility.Localizeable
{
/// <summary>
/// Description of LocalizableDescriptionAttribute.

0 comments on commit 7bc2c5d

Please sign in to comment.
Something went wrong with that request. Please try again.