Browse Source

Combobox with Commands

pull/20/merge
PeterForstmeier 15 years ago
parent
commit
7bc2c5db2d
  1. 7
      src/AddIns/Analysis/CodeQuality/CodeQualityAnalysis.csproj
  2. 27
      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 @@
<Compile Include="Src\Relationship.cs" /> <Compile Include="Src\Relationship.cs" />
<Compile Include="Src\RelationshipType.cs" /> <Compile Include="Src\RelationshipType.cs" />
<Compile Include="Src\Type.cs" /> <Compile Include="Src\Type.cs" />
<Compile Include="Src\Utility\ComboBoxWithCommand.cs" />
<Compile Include="Src\Utility\DoubleKeyDictionary.cs" /> <Compile Include="Src\Utility\DoubleKeyDictionary.cs" />
<Compile Include="Src\Utility\IColorizer.cs" /> <Compile Include="Src\Utility\IColorizer.cs" />
<Compile Include="Src\Utility\Helper.cs" /> <Compile Include="Src\Utility\Helper.cs" />
<Compile Include="Src\Utility\LocalizeableCombo\EnumToFriendlyNameConverter.cs" /> <Compile Include="Src\Utility\Localizeable\EnumToFriendlyNameConverter.cs" />
<Compile Include="Src\Utility\LocalizeableCombo\LocalizableDescriptionAttribute.cs" /> <Compile Include="Src\Utility\Localizeable\LocalizableDescriptionAttribute.cs" />
<Compile Include="Src\Utility\Matrix.cs" /> <Compile Include="Src\Utility\Matrix.cs" />
<Compile Include="Src\Utility\RelayCommand.cs" /> <Compile Include="Src\Utility\RelayCommand.cs" />
<Compile Include="Src\Utility\ViewModelBase.cs" /> <Compile Include="Src\Utility\ViewModelBase.cs" />
@ -270,7 +271,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Src\Utility" /> <Folder Include="Src\Utility" />
<Folder Include="Src\Utility\LocalizeableCombo" /> <Folder Include="Src\Utility\Localizeable" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.

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

@ -12,7 +12,8 @@
xmlns:tree="http://icsharpcode.net/sharpdevelop/treeview" xmlns:tree="http://icsharpcode.net/sharpdevelop/treeview"
xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:t="clr-namespace:TreeMaps.Controls;assembly=TreeMaps" 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}" Title="{Binding FrmTitle}"
x:Name="root"> x:Name="root">
@ -279,16 +280,18 @@
Grid.Column="0"> Grid.Column="0">
<ToolBar> <ToolBar>
<TextBlock Text="Level: " /> <TextBlock Text="Level: " />
<!-- SelectedItem="{Binding MetricsLevelSelectedItem}"-->
<ComboBox Width="200" <util:ComboBoxWithCommand Width="200"
ItemsSource="{Binding Source={StaticResource MetricsLevel}}"></ComboBox> ItemsSource="{Binding Source={StaticResource MetricsLevel}}"
Command="{Binding ActivateMetrics}">
</util:ComboBoxWithCommand>
<TextBlock Margin="4,0,0,0" Text="Metric: " /> <TextBlock Margin="4,0,0,0" Text="Metric: " />
<ComboBox Width="200" Margin="4,0,0,0" <util:ComboBoxWithCommand Width="200" Margin="4,0,0,0"
ItemsSource="{Binding Source={StaticResource Metrics}}" ItemsSource="{Binding Source={StaticResource Metrics}}"
SelectedItem="{Binding SelectedMetrics}"> Command="{Binding ShowTreeMap}">
<ComboBox.ItemTemplate> <util:ComboBoxWithCommand.ItemTemplate>
<DataTemplate> <DataTemplate>
<Label Content="{Binding Path=.,Mode=OneWay, <Label Content="{Binding Path=.,Mode=OneWay,
Converter={StaticResource enumItemsConverter}}" Converter={StaticResource enumItemsConverter}}"
@ -297,10 +300,10 @@
VerticalAlignment="Center"/> VerticalAlignment="Center"/>
</DataTemplate> </DataTemplate>
</ComboBox.ItemTemplate> </util:ComboBoxWithCommand.ItemTemplate>
</ComboBox> </util:ComboBoxWithCommand>
</ToolBar> </ToolBar>
</ToolBarTray> </ToolBarTray>
<!-- http://treemaps.codeplex.com/--> <!-- http://treemaps.codeplex.com/-->

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

@ -16,7 +16,7 @@ using System.Windows.Media.Imaging;
using ICSharpCode.CodeQualityAnalysis.Controls; using ICSharpCode.CodeQualityAnalysis.Controls;
using ICSharpCode.CodeQualityAnalysis.Utility; using ICSharpCode.CodeQualityAnalysis.Utility;
using ICSharpCode.CodeQualityAnalysis.Utility.LocalizeableCombo; using ICSharpCode.CodeQualityAnalysis.Utility.Localizeable;
using Microsoft.Win32; using Microsoft.Win32;
namespace ICSharpCode.CodeQualityAnalysis namespace ICSharpCode.CodeQualityAnalysis
@ -50,16 +50,19 @@ namespace ICSharpCode.CodeQualityAnalysis
public MainWindowViewModel():base() public MainWindowViewModel():base()
{ {
this.FrmTitle = "$Code Quality Analysis"; this.FrmTitle = "Code Quality Analysis";
this.btnOpenAssembly = "$Open Assembly"; this.btnOpenAssembly = "Open Assembly";
#region MainTab #region MainTab
this.TabDependencyGraph = "$Dependency Graph"; this.TabDependencyGraph = "Dependency Graph";
this.TabDependencyMatrix = "$Dependency Matrix"; this.TabDependencyMatrix = "Dependency Matrix";
this.TabMetrics = "$Metrics"; this.TabMetrics = "Metrics";
#endregion #endregion
MetrixTabEnable = false; MetrixTabEnable = false;
ActivateMetrics = new RelayCommand(ActivateMetricsExecute);
ShowTreeMap = new RelayCommand(ShowTreemapExecute,CanActivateTreemap);
} }
@ -147,10 +150,6 @@ namespace ICSharpCode.CodeQualityAnalysis
base.RaisePropertyChanged(() =>this.Nodes);} 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 ; private string treeValueProperty ;
@ -160,22 +159,27 @@ namespace ICSharpCode.CodeQualityAnalysis
base.RaisePropertyChanged(() =>this.TreeValueProperty);} base.RaisePropertyChanged(() =>this.TreeValueProperty);}
} }
// First Combo // MetricsLevel Combo
public MetricsLevel MetricsLevel { public MetricsLevel MetricsLevel {
get {return MetricsLevel;} get {return MetricsLevel;}
} }
// MetricsLevel metricsLevelSelectedItem = MetricsLevel.Assembly; #region ActivateMetrics
//
// public MetricsLevel MetricsLevelSelectedItem { public ICommand ActivateMetrics {get;private set;}
// get { return metricsLevelSelectedItem; }
// set { metricsLevelSelectedItem = value; bool metricsIsActive;
// base.RaisePropertyChanged(() =>this.MetricsLevelSelectedItem);
// } void ActivateMetricsExecute ()
// } {
metricsIsActive = true;
}
#endregion
// Second Combo // Metrics Combo
public Metrics Metrics public Metrics Metrics
{ {
@ -188,12 +192,19 @@ namespace ICSharpCode.CodeQualityAnalysis
get { return selectedMetrics; } get { return selectedMetrics; }
set { selectedMetrics = value; set { selectedMetrics = value;
base.RaisePropertyChanged(() =>this.SelectedMetrics); 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 var r = from ns in MainModule.Namespaces
from type in ns.Types from type in ns.Types
@ -217,5 +228,7 @@ namespace ICSharpCode.CodeQualityAnalysis
throw new Exception("Invalid value for Metrics"); throw new Exception("Invalid value for Metrics");
} }
} }
#endregion
} }
} }

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);
}
}
}
}
}

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;
using System.Reflection; using System.Reflection;
using System.Runtime.Serialization; using System.Runtime.Serialization;
namespace ICSharpCode.CodeQualityAnalysis.Utility.LocalizeableCombo namespace ICSharpCode.CodeQualityAnalysis.Utility.Localizeable
{ {
/// <summary> /// <summary>
/// Description of EnumToFriendlyNameConverter. /// 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 @@
using System; using System;
using System.ComponentModel; using System.ComponentModel;
namespace ICSharpCode.CodeQualityAnalysis.Utility.LocalizeableCombo namespace ICSharpCode.CodeQualityAnalysis.Utility.Localizeable
{ {
/// <summary> /// <summary>
/// Description of LocalizableDescriptionAttribute. /// Description of LocalizableDescriptionAttribute.
Loading…
Cancel
Save