Browse Source

Combobox with Commands

pull/20/merge
PeterForstmeier 15 years ago
parent
commit
7bc2c5db2d
  1. 7
      src/AddIns/Analysis/CodeQuality/CodeQualityAnalysis.csproj
  2. 25
      src/AddIns/Analysis/CodeQuality/Src/MainWindow.xaml
  3. 57
      src/AddIns/Analysis/CodeQuality/Src/MainWindowModel.cs
  4. 164
      src/AddIns/Analysis/CodeQuality/Src/Utility/ComboBoxWithCommand.cs
  5. 2
      src/AddIns/Analysis/CodeQuality/Src/Utility/Localizeable/EnumToFriendlyNameConverter.cs
  6. 2
      src/AddIns/Analysis/CodeQuality/Src/Utility/Localizeable/LocalizableDescriptionAttribute.cs

7
src/AddIns/Analysis/CodeQuality/CodeQualityAnalysis.csproj

@ -142,11 +142,12 @@ @@ -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 @@ @@ -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.

25
src/AddIns/Analysis/CodeQuality/Src/MainWindow.xaml

@ -12,7 +12,8 @@ @@ -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 @@ @@ -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 @@ @@ -297,10 +300,10 @@
VerticalAlignment="Center"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</ToolBar>
</util:ComboBoxWithCommand.ItemTemplate>
</util:ComboBoxWithCommand>
</ToolBar>
</ToolBarTray>
<!-- http://treemaps.codeplex.com/-->

57
src/AddIns/Analysis/CodeQuality/Src/MainWindowModel.cs

@ -16,7 +16,7 @@ using System.Windows.Media.Imaging; @@ -16,7 +16,7 @@ using System.Windows.Media.Imaging;
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 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -50,16 +50,19 @@ namespace ICSharpCode.CodeQualityAnalysis
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 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -147,10 +150,6 @@ namespace ICSharpCode.CodeQualityAnalysis
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 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -160,22 +159,27 @@ namespace ICSharpCode.CodeQualityAnalysis
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 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -188,12 +192,19 @@ namespace ICSharpCode.CodeQualityAnalysis
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 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -217,5 +228,7 @@ namespace ICSharpCode.CodeQualityAnalysis
throw new Exception("Invalid value for Metrics");
}
}
#endregion
}
}

164
src/AddIns/Analysis/CodeQuality/Src/Utility/ComboBoxWithCommand.cs

@ -0,0 +1,164 @@ @@ -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);
}
}
}
}
}

2
src/AddIns/Analysis/CodeQuality/Src/Utility/LocalizeableCombo/EnumToFriendlyNameConverter.cs → src/AddIns/Analysis/CodeQuality/Src/Utility/Localizeable/EnumToFriendlyNameConverter.cs

@ -12,7 +12,7 @@ using System.Globalization; @@ -12,7 +12,7 @@ using System.Globalization;
using System.Reflection;
using System.Runtime.Serialization;
namespace ICSharpCode.CodeQualityAnalysis.Utility.LocalizeableCombo
namespace ICSharpCode.CodeQualityAnalysis.Utility.Localizeable
{
/// <summary>
/// Description of EnumToFriendlyNameConverter.

2
src/AddIns/Analysis/CodeQuality/Src/Utility/LocalizeableCombo/LocalizableDescriptionAttribute.cs → src/AddIns/Analysis/CodeQuality/Src/Utility/Localizeable/LocalizableDescriptionAttribute.cs

@ -9,7 +9,7 @@ @@ -9,7 +9,7 @@
using System;
using System.ComponentModel;
namespace ICSharpCode.CodeQualityAnalysis.Utility.LocalizeableCombo
namespace ICSharpCode.CodeQualityAnalysis.Utility.Localizeable
{
/// <summary>
/// Description of LocalizableDescriptionAttribute.
Loading…
Cancel
Save