This commit is contained in:
艾竹
2022-12-12 22:33:17 +08:00
parent 02f428d61a
commit 4b798f75a0
60 changed files with 330 additions and 277 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

@@ -2,11 +2,23 @@
<PropertyGroup>
<UseWPF>true</UseWPF>
<Company>AIStudio.Wpf.Controls</Company>
<Authors>akwkevin</Authors>
<PackageProjectUrl>https://gitee.com/akwkevin</PackageProjectUrl>
<PackageIcon>A.png</PackageIcon>
<PackageIconUrl />
<NeutralLanguage />
<Version>1.0.1</Version>
<Description>一个Wpf的Diagram控件基础库</Description>
</PropertyGroup>
<ItemGroup>
<None Remove="Images\file.png" />
<None Remove="Images\FormatPainter.cur" />
<None Include="A.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>
<ItemGroup>
@@ -32,6 +44,15 @@
</ItemGroup>
<ItemGroup>
<Page Update="Controls\MultiSelectComboBox.xaml">
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
</Page>
<Page Update="Controls\PopupWindow.xaml">
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
</Page>
<Page Update="Controls\PropertiesView.xaml">
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
</Page>
<Page Update="Styles\ComboBox.xaml">
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
<SubType>Designer</SubType>

View File

@@ -0,0 +1,103 @@
<UserControl x:Class="AIStudio.Wpf.DiagramDesigner.Controls.MultiSelectComboBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ComboBox Background="Transparent" BorderBrush="Transparent"
x:Name="MultiSelectCombo"
SnapsToDevicePixels="True"
OverridesDefaultStyle="True"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.CanContentScroll="True"
IsSynchronizedWithCurrentItem="True">
<ComboBox.Resources>
<Style TargetType="ComboBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ComboBox.Resources>
<ComboBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Title}"
IsChecked="{Binding Path=IsSelected, Mode=TwoWay}"
Tag="{RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}"
Click="CheckBox_Click" />
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.Template>
<ControlTemplate TargetType="ComboBox">
<Grid >
<ToggleButton x:Name="ToggleButton" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"
Grid.Column="2" IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
Focusable="false"
ClickMode="Press" HorizontalContentAlignment="Left" >
<ToggleButton.Template>
<ControlTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="12"/>
</Grid.ColumnDefinitions>
<Border
x:Name="Border"
Grid.ColumnSpan="2"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}" />
<Border
x:Name="BorderComp"
Grid.Column="0"
CornerRadius="2"
Margin="1"
BorderThickness="0,0,0,0" >
<TextBlock Text="{Binding Path=Text,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" Padding="3" />
</Border>
<Border x:Name="ArrowBorder" Grid.Column="1" >
<Path
x:Name="Arrow"
Stretch="Fill" Width="5" Height="3"
Fill="Black"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Data="M 0 0 L 4 4 L 8 0 Z"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="ArrowBorder" Property="Background" Value="{DynamicResource Fluent.Ribbon.Brushes.Button.MouseOver.Background}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ToggleButton.Template>
</ToggleButton>
<Popup x:Name="Popup"
Placement="Bottom"
AllowsTransparency="True"
Focusable="False" IsOpen="{TemplateBinding IsDropDownOpen}"
PopupAnimation="Slide">
<Grid x:Name="DropDown"
SnapsToDevicePixels="True"
MinWidth="{TemplateBinding ActualWidth}"
MaxHeight="{TemplateBinding MaxDropDownHeight}">
<Border x:Name="DropDownBorder"
BorderThickness="1" Background="{DynamicResource WhiteBrush}"
BorderBrush="{DynamicResource GrayBrush8}"/>
<ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True" DataContext="{Binding}">
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
</ScrollViewer>
</Grid>
</Popup>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="HasItems" Value="false">
<Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
</Trigger>
<Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
<Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0"/>
</Trigger>
<Trigger Property="IsDropDownOpen" Value="true">
<Setter TargetName="ToggleButton" Property="Background" Value="{DynamicResource Fluent.Ribbon.Brushes.Button.MouseOver.Background}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ComboBox.Template>
</ComboBox>
</UserControl>

View File

@@ -0,0 +1,328 @@
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
namespace AIStudio.Wpf.DiagramDesigner.Controls
{
/// <summary>
/// Interaction logic for MultiSelectComboBox.xaml
/// </summary>
public partial class MultiSelectComboBox : UserControl
{
private ObservableCollection<Node> _nodeList;
public MultiSelectComboBox()
{
InitializeComponent();
_nodeList = new ObservableCollection<Node>();
}
#region Dependency Properties
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register(nameof(ItemsSource), typeof(IList), typeof(MultiSelectComboBox), new FrameworkPropertyMetadata(null,
new PropertyChangedCallback(MultiSelectComboBox.OnItemsSourceChanged)));
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.Register(nameof(SelectedItems), typeof(IList), typeof(MultiSelectComboBox), new FrameworkPropertyMetadata(null,
new PropertyChangedCallback(MultiSelectComboBox.OnSelectedItemsChanged)));
public static readonly DependencyProperty SelectedValuesProperty =
DependencyProperty.Register(nameof(SelectedValues), typeof(IList), typeof(MultiSelectComboBox), new FrameworkPropertyMetadata(null,
new PropertyChangedCallback(MultiSelectComboBox.OnSelectedValuesChanged)));
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register(nameof(Text), typeof(string), typeof(MultiSelectComboBox), new UIPropertyMetadata(string.Empty));
public static readonly DependencyProperty DefaultTextProperty =
DependencyProperty.Register(nameof(DefaultText), typeof(string), typeof(MultiSelectComboBox), new UIPropertyMetadata(string.Empty));
public string DisplayMemberPath { get; set; }
public string SelectedValuePath { get; set; }
public IList ItemsSource
{
get { return (IList)GetValue(ItemsSourceProperty); }
set
{
SetValue(ItemsSourceProperty, value);
}
}
public IList SelectedItems
{
get { return (IList)GetValue(SelectedItemsProperty); }
set
{
SetValue(SelectedItemsProperty, value);
}
}
public IList SelectedValues
{
get { return (IList)GetValue(SelectedValuesProperty); }
set
{
SetValue(SelectedValuesProperty, value);
}
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public string DefaultText
{
get { return (string)GetValue(DefaultTextProperty); }
set { SetValue(DefaultTextProperty, value); }
}
#endregion
#region Events
private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MultiSelectComboBox control = (MultiSelectComboBox)d;
control.DisplayInControl();
control.SelectNodes();
control.SetText();
}
private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MultiSelectComboBox control = (MultiSelectComboBox)d;
control.SelectNodes();
control.SetText();
}
private static void OnSelectedValuesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MultiSelectComboBox control = (MultiSelectComboBox)d;
control.SelectNodes();
control.SetText();
}
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
CheckBox clickedBox = (CheckBox)sender;
if (clickedBox.Content.ToString() == "All")
{
if (clickedBox.IsChecked.Value)
{
foreach (Node node in _nodeList)
{
node.IsSelected = true;
}
}
else
{
foreach (Node node in _nodeList)
{
node.IsSelected = false;
}
}
}
else
{
int _selectedCount = 0;
foreach (Node s in _nodeList)
{
if (s.IsSelected && s.Object.ToString() != "All")
_selectedCount++;
}
if (_selectedCount == _nodeList.Count - 1)
_nodeList.FirstOrDefault(i => i.Object.ToString() == "All").IsSelected = true;
else
_nodeList.FirstOrDefault(i => i.Object.ToString() == "All").IsSelected = false;
}
SetSelectedItems();
SetText();
}
#endregion
#region Methods
private void SelectNodes()
{
if (SelectedItems != null)
{
foreach (var item in SelectedItems)
{
Node node = _nodeList.FirstOrDefault(i => i.Object == item);
if (node != null)
node.IsSelected = true;
}
}
else if (SelectedValues != null)
{
foreach (var item in SelectedValues)
{
Node node = _nodeList.FirstOrDefault(i => i.Object != null && i.Object.ToString() != "All" && i.Object.GetPropertyValue(SelectedValuePath)?.ToString() == item?.ToString());
if (node != null)
node.IsSelected = true;
}
}
}
private void SetSelectedItems()
{
if (SelectedItems != null)
{
SelectedItems.Clear();
foreach (Node node in _nodeList)
{
if (node.IsSelected && node.Object.ToString() != "All")
{
if (this.ItemsSource.Count > 0)
{
if (SelectedItems != null)
{
SelectedItems.Add(node.Object);
}
}
}
}
}
if (SelectedValues != null)
{
SelectedValues.Clear();
foreach (Node node in _nodeList)
{
if (node.IsSelected && node.Object.ToString() != "All")
{
if (this.ItemsSource.Count > 0)
{
if (SelectedValues != null)
{
SelectedValues.Add(node.Object.GetPropertyValue(SelectedValuePath));
}
}
}
}
}
}
private void DisplayInControl()
{
_nodeList.Clear();
if (this.ItemsSource.Count > 0)
_nodeList.Add(new Node("All", DisplayMemberPath));
foreach (var item in this.ItemsSource)
{
Node node = new Node(item, DisplayMemberPath);
_nodeList.Add(node);
}
MultiSelectCombo.ItemsSource = _nodeList;
}
private void SetText()
{
StringBuilder displayText = new StringBuilder();
foreach (Node s in _nodeList)
{
//不使用ALl来显示
//if (s.IsSelected == true && s.Object.ToString() == "All")
//{
// displayText = new StringBuilder();
// displayText.Append("All");
// break;
//}
//else
if (s.IsSelected == true && s.Object.ToString() != "All")
{
displayText.Append(s.Object);
displayText.Append(',');
}
}
this.Text = displayText.ToString().TrimEnd(new char[] { ',' });
// set DefaultText if nothing else selected
if (string.IsNullOrEmpty(this.Text))
{
this.Text = this.DefaultText;
}
}
#endregion
}
public class Node : INotifyPropertyChanged
{
#region ctor
public Node(object obj, string displayMemberPath)
{
Object = obj;
if (!string.IsNullOrEmpty(displayMemberPath) && Object.ContainsProperty(displayMemberPath))
Title = Object.GetPropertyValue(displayMemberPath).ToString();
else
Title = obj.ToString();
}
#endregion
#region Properties
private object _object;
public object Object
{
get
{
return _object;
}
set
{
_object = value;
NotifyPropertyChanged("Object");
}
}
private string _title;
public string Title
{
get
{
return _title;
}
set
{
_title = value;
NotifyPropertyChanged("Title");
}
}
private bool _isSelected;
public bool IsSelected
{
get
{
return _isSelected;
}
set
{
_isSelected = value;
NotifyPropertyChanged("IsSelected");
}
}
#endregion
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}

View File

@@ -0,0 +1,34 @@
<Window x:Class="AIStudio.Wpf.DiagramDesigner.Controls.PopupWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="{Binding Title}"
SizeToContent="WidthAndHeight"
WindowStartupLocation="CenterOwner"
x:Name="theView">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ContentControl Grid.Row="0"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Content="{Binding ElementName=theView, Path=DataContext}" />
<StackPanel Grid.Row="1"
Orientation="Horizontal"
HorizontalAlignment="Right">
<Button Content="Ok"
IsDefault="True"
Click="Ok_Click"
Margin="5"
Width="100"
Height="30" />
<Button Content="Cancel"
IsCancel="True"
Margin="5"
Width="100"
Height="30" />
</StackPanel>
</Grid>
</Window>

View File

@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace AIStudio.Wpf.DiagramDesigner.Controls
{
/// <summary>
/// PopupWindow.xaml 的交互逻辑
/// </summary>
public partial class PopupWindow : Window
{
public PopupWindow()
{
InitializeComponent();
}
private void Ok_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
this.Close();
}
}
}

View File

@@ -0,0 +1,63 @@
<UserControl x:Class="AIStudio.Wpf.DiagramDesigner.Controls.PropertiesView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Border x:Name="border" BorderThickness="1" BorderBrush="{DynamicResource GrayBrush8}">
<Border.Resources>
<ControlTemplate x:Key="validationErrorTemplate">
<DockPanel>
<Image Source="/AIStudio.Wpf.DiagramHelper;component/Images/error.png" Height="16" Width="16"
DockPanel.Dock="Right" Margin="-18,0,0,0"
ToolTip="{Binding ElementName=adorner,
Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
</Image>
<AdornedElementPlaceholder x:Name="adorner"/>
</DockPanel>
</ControlTemplate>
<Style x:Key="gridLineStyle" TargetType="Line">
<Setter Property="Stroke" Value="{DynamicResource GrayBrush8}" />
<Setter Property="Stretch" Value="Fill" />
<Setter Property="Grid.ZIndex" Value="1000" />
</Style>
<Style x:Key="gridHorizontalLineStyle" TargetType="Line" BasedOn="{StaticResource gridLineStyle}">
<Setter Property="X2" Value="1" />
<Setter Property="VerticalAlignment" Value="Bottom" />
<Setter Property="Grid.ColumnSpan"
Value="{Binding
Path=ColumnDefinitions.Count,
RelativeSource={RelativeSource AncestorType=Grid}}"/>
</Style>
<Style x:Key="gridVerticalLineStyle" TargetType="Line" BasedOn="{StaticResource gridLineStyle}">
<Setter Property="Y2" Value="1" />
<Setter Property="HorizontalAlignment" Value="Right" />
<Setter Property="Grid.RowSpan"
Value="{Binding
Path=RowDefinitions.Count,
RelativeSource={RelativeSource AncestorType=Grid}}"/>
</Style>
</Border.Resources>
<DockPanel x:Name="_panel">
<Border x:Name="_label" Width="50" Height="16">
<TextBlock Text="Empty" TextAlignment="Center" Foreground="Gray"/>
</Border>
<ScrollViewer x:Name="_gridContainer" VerticalScrollBarVisibility="Auto">
<Grid x:Name="_grid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Line Name="_vLine" Grid.Column="0" Grid.RowSpan="1000" Style="{StaticResource gridVerticalLineStyle}"/>
<GridSplitter Name="_splitter" Grid.RowSpan="1000" Margin="0,0,0,0" Width="1"
Background="{DynamicResource GrayBrush8}" Grid.ZIndex="10000"/>
</Grid>
</ScrollViewer>
</DockPanel>
</Border>
</UserControl>

View File

@@ -0,0 +1,263 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Reflection;
namespace AIStudio.Wpf.DiagramDesigner.Controls
{
/// <summary>
/// Interaction logic for PropertiesView.xaml
/// </summary>
public partial class PropertiesView : UserControl
{
#region SelectedObject
public static readonly DependencyProperty SelectedObjectProperty = DependencyProperty.Register("SelectedObject", typeof(object), typeof(PropertiesView), new UIPropertyMetadata(null, OnSelectedObjectChanged));
public object SelectedObject
{
get
{
return (object)GetValue(SelectedObjectProperty);
}
set
{
SetValue(SelectedObjectProperty, value);
}
}
private static void OnSelectedObjectChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
PropertiesView propertyInspector = o as PropertiesView;
if (propertyInspector != null)
propertyInspector.OnSelectedObjectChanged((object)e.OldValue, (object)e.NewValue);
}
protected virtual void OnSelectedObjectChanged(object oldValue, object newValue)
{
// We do not want to process the change now if the grid is initializing (ie. BeginInit/EndInit).
var obj = oldValue as INotifyPropertyChanged;
if (obj != null)
obj.PropertyChanged -= PropertyChanged;
DisplayProperties();
obj = newValue as INotifyPropertyChanged;
if (obj != null)
obj.PropertyChanged += PropertyChanged;
}
#endregion //SelectedObject
public static readonly DependencyProperty NeedBrowsableProperty = DependencyProperty.Register("NeedBrowsable", typeof(bool), typeof(PropertiesView), new UIPropertyMetadata(true));
public bool NeedBrowsable
{
get
{
return (bool)GetValue(NeedBrowsableProperty);
}
set
{
SetValue(NeedBrowsableProperty, value);
}
}
public bool CustomSetting
{
get;
set;
}
public PropertiesView()
{
InitializeComponent();
this.Loaded += PropertiesView_Loaded;
}
private void PropertiesView_Loaded(object sender, RoutedEventArgs e)
{
DisplayProperties();
}
void PropertyChanged(object sender, PropertyChangedEventArgs e)
{
DisplayProperties();
}
private void DisplayProperties()
{
_panel.Children.Clear();
ClearGrid();
if (SelectedObject != null)
{
if (CustomSetting == true)
{
if (SelectedObject.GetType().GetProperty("PropertiesSetting") != null)
{
Dictionary<string, string> settings = SelectedObject.GetType().GetProperty("PropertiesSetting").GetValue(SelectedObject) as Dictionary<string, string>;
int row = 0;
foreach (var setting in settings)
{
var prop = SelectedObject.GetType().GetProperty(setting.Key);
DisplayProperty(prop, row, setting.Value);
row++;
}
}
}
else
{
int row = 0;
foreach (var prop in SelectedObject.GetType().GetProperties())
{
var attr = prop.GetCustomAttributes(typeof(BrowsableAttribute), true);
if (NeedBrowsable == false && (attr.Length == 0 || (attr[0] as BrowsableAttribute).Browsable))
{
DisplayProperty(prop, row);
row++;
}
else if (NeedBrowsable == true && (attr.Length > 0 && (attr[0] as BrowsableAttribute).Browsable))
{
DisplayProperty(prop, row);
row++;
}
}
}
_panel.Children.Add(_gridContainer);
}
else
{
_panel.Children.Add(_label);
}
}
private void ClearGrid()
{
_grid.RowDefinitions.Clear();
for (int i = _grid.Children.Count - 1; i >= 0; i--)
{
if (_grid.Children[i] != _vLine && _grid.Children[i] != _splitter)
_grid.Children.RemoveAt(i);
}
}
private void DisplayProperty(PropertyInfo prop, int row, string name = null)
{
var rowDef = new RowDefinition();
rowDef.Height = new GridLength(Math.Max(20, this.FontSize * 2));
_grid.RowDefinitions.Add(rowDef);
var tb = new TextBlock() { Text = prop.Name };
if (name != null)
{
tb.Text = name;
tb.ToolTip = prop.Name;
}
else
{
var displayAttr = prop.GetCustomAttributes(typeof(DisplayNameAttribute), true);
if (displayAttr.Length > 0)
{
tb.Text = (displayAttr[0] as DisplayNameAttribute).DisplayName;
tb.ToolTip = prop.Name;
}
}
tb.Margin = new Thickness(4);
Grid.SetColumn(tb, 0);
Grid.SetRow(tb, _grid.RowDefinitions.Count - 1);
_grid.Children.Add(tb);
var line = new Line();
line.Style = (Style)border.Resources["gridHorizontalLineStyle"];
Grid.SetRow(line, row);
_grid.Children.Add(line);
Style style = null;
var styleNameAttr = prop.GetCustomAttributes(typeof(StyleNameAttribute), true);
if (styleNameAttr.Length > 0)
{
style = this.FindResource((styleNameAttr[0] as StyleNameAttribute).Name) as Style;
if (style != null)
{
ContentControl content = new ContentControl();
content.Style = style;
content.DataContext = SelectedObject;
Grid.SetColumn(content, 1);
Grid.SetRow(content, _grid.RowDefinitions.Count - 1);
_grid.Children.Add(content);
}
}
if (style == null)
{
var ed = new TextBox();
ed.PreviewKeyDown += new KeyEventHandler(ed_KeyDown);
ed.Margin = new Thickness(0);
ed.VerticalAlignment = VerticalAlignment.Center;
ed.BorderThickness = new Thickness(0);
Grid.SetColumn(ed, 1);
Grid.SetRow(ed, _grid.RowDefinitions.Count - 1);
var binding = new Binding(prop.Name);
binding.Source = SelectedObject;
binding.ValidatesOnExceptions = true;
binding.Mode = BindingMode.OneWay;
if (prop.CanWrite)
{
var mi = prop.GetSetMethod();
if (mi != null && mi.IsPublic)
binding.Mode = BindingMode.TwoWay;
}
ed.SetBinding(TextBox.TextProperty, binding);
var template = (ControlTemplate)border.Resources["validationErrorTemplate"];
Validation.SetErrorTemplate(ed, template);
_grid.Children.Add(ed);
}
}
void ed_KeyDown(object sender, KeyEventArgs e)
{
var ed = sender as TextBox;
if (ed != null)
{
if (e.Key == Key.Enter)
{
ed.GetBindingExpression(TextBox.TextProperty).UpdateSource();
e.Handled = true;
}
else if (e.Key == Key.Escape)
ed.GetBindingExpression(TextBox.TextProperty).UpdateTarget();
}
}
}
[AttributeUsage(AttributeTargets.Property)]
public class StyleNameAttribute : Attribute
{
private string _name;
public StyleNameAttribute(string name)
{
_name = name;
}
public string Name
{
get
{
return _name;
}
}
}
}

View File

@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
namespace AIStudio.Wpf.DiagramDesigner.Converters
{
public class BoolVisibilityConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var visibility = value as bool?;
var visible = parameter as string;
if (visibility != null && visible != null)
{
if (visibility == true && visible == "true")
{
return Visibility.Visible;
}
if (visibility == false && visible == "false")
{
return Visibility.Visible;
}
}
else if (visibility != null)
{
if (visibility == true)
{
return Visibility.Visible;
}
}
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
#endregion
}
}

View File

@@ -0,0 +1,81 @@
using System;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;
namespace AIStudio.Wpf.DiagramDesigner.Converters
{
public class ConverterBoolToValueMap : MarkupExtension, IValueConverter
{
public override object ProvideValue(System.IServiceProvider serviceProvider)
{
return new ConverterBoolToValueMap
{
FromType = this.FromType ?? typeof(char),
TargetType = this.TargetType ?? typeof(bool),
Parameter = this.Parameter
};
}
public object Parameter
{
get;
set;
}
public Type TargetType
{
get;
set;
}
public Type FromType
{
get;
set;
}
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var result = DependencyProperty.UnsetValue;
if (value == null) return result;
if (value is bool)
{
if ((bool)value)
result = parameter;
else
result = this.Parameter;
if (result == null || string.IsNullOrWhiteSpace(result.ToString()))
return DependencyProperty.UnsetValue;
//处理枚举类型
if (targetType.IsEnum)
result = Enum.Parse(targetType, result as string);
else if (targetType.IsValueType)
{
if (targetType == typeof(Int32))
result = System.Convert.ToInt32(result);
else if (targetType == typeof(Int16))
result = System.Convert.ToInt16(result);
else if (targetType == typeof(char))
result = result.ToString().First();
}
}
return result;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null || parameter == null)
return false;
if (object.Equals(value, parameter))
return true;
return string.Equals(value.ToString(), parameter.ToString());
}
#endregion
}
}

View File

@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;
namespace AIStudio.Wpf.DiagramDesigner.Converters
{
public class ConverterValueMapSetToVisibility : MarkupExtension, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null || parameter == null)
return DependencyProperty.UnsetValue;
IEnumerable<string> checkList = parameter.ToString().Split('^');
bool equal = checkList.Contains(value.ToString());
if (equal)
return Visibility.Visible;
else
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
public override object ProvideValue(System.IServiceProvider serviceProvider)
{
return new ConverterValueMapSetToVisibility
{
FromType = this.FromType ?? typeof(char),
TargetType = this.TargetType ?? typeof(bool),
Parameter = this.Parameter
};
}
public object Parameter
{
get;
set;
}
public Type TargetType
{
get;
set;
}
public Type FromType
{
get;
set;
}
}
}

View File

@@ -0,0 +1,81 @@
using System;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;
namespace AIStudio.Wpf.DiagramDesigner.Converters
{
public class ConverterValueMapToBool : MarkupExtension, IValueConverter
{
public override object ProvideValue(System.IServiceProvider serviceProvider)
{
return new ConverterValueMapToBool
{
FromType = this.FromType ?? typeof(char),
TargetType = this.TargetType ?? typeof(bool),
Parameter = this.Parameter
};
}
public object Parameter
{
get;
set;
}
public Type TargetType
{
get;
set;
}
public Type FromType
{
get;
set;
}
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null || parameter == null)
return false;
if (object.Equals(value, parameter))
return true;
return string.Equals(value.ToString(), parameter.ToString());
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var result = DependencyProperty.UnsetValue;
if (value == null) return result;
if (value is bool)
{
if ((bool)value)
result = parameter;
else
result = this.Parameter;
if (result == null || string.IsNullOrWhiteSpace(result.ToString()))
return DependencyProperty.UnsetValue;
//处理枚举类型
if (targetType.IsEnum)
result = Enum.Parse(targetType, result as string);
else if (targetType.IsValueType)
{
if (targetType == typeof(Int32))
result = System.Convert.ToInt32(result);
else if (targetType == typeof(Int16))
result = System.Convert.ToInt16(result);
else if (targetType == typeof(char))
result = result.ToString().First();
}
}
return result;
}
#endregion
}
}

View File

@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;
namespace AIStudio.Wpf.DiagramDesigner.Converters
{
public class ConverterValueMapToVisibility : MarkupExtension, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null || parameter == null)
return DependencyProperty.UnsetValue;
bool equal = string.Equals(value.ToString(), parameter.ToString());
//true = Visible
if (equal)
return Visibility.Visible;
else
return this.Parameter != null && this.Parameter.ToString().ToLower() == Visibility.Hidden.ToString().ToLower() ? Visibility.Hidden : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
public override object ProvideValue(System.IServiceProvider serviceProvider)
{
return new ConverterValueMapToVisibility
{
FromType = this.FromType ?? typeof(char),
TargetType = this.TargetType ?? typeof(bool),
Parameter = this.Parameter
};
}
public object Parameter
{
get;
set;
}
public Type TargetType
{
get;
set;
}
public Type FromType
{
get;
set;
}
}
}

View File

@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;
namespace AIStudio.Wpf.DiagramDesigner.Converters
{
public class ConverterValueSetToOppositeVisibility : MarkupExtension, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null || parameter == null)
return DependencyProperty.UnsetValue;
IEnumerable<string> checkList = parameter.ToString().Split('^');
bool equal = checkList.Contains(value.ToString());
if (equal)
return Visibility.Collapsed;
else
return Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
public override object ProvideValue(System.IServiceProvider serviceProvider)
{
return new ConverterValueSetToOppositeVisibility
{
FromType = this.FromType ?? typeof(char),
TargetType = this.TargetType ?? typeof(bool),
Parameter = this.Parameter
};
}
public object Parameter
{
get;
set;
}
public Type TargetType
{
get;
set;
}
public Type FromType
{
get;
set;
}
}
}

View File

@@ -0,0 +1,176 @@
using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace AIStudio.Wpf.DiagramDesigner.Converters
{
#region Half
public class HalfConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var doubleValue = (value as double?).GetValueOrDefault();
return doubleValue / 2;
}
public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
}
#endregion
#region String IsNullOrEmpty
public class IsNullOrEmptyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return string.IsNullOrEmpty((string)value);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
}
#endregion
#region True -> False
public class BoolInverseConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return !(bool)value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
}
#endregion
#region True -> Visible Or Collpased
public class BoolToVisibleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
}
public class BoolToInvisibleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? Visibility.Collapsed : Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
}
#endregion
#region object try to convert to image (if is uri)
public class IconConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || !(value.GetType() == typeof(string)))
return value;
var iconString = value as string;
if (!string.IsNullOrEmpty(iconString) && Uri.IsWellFormedUriString(iconString, UriKind.RelativeOrAbsolute))
{
var image = new System.Windows.Controls.Image()
{
Source = new System.Windows.Media.Imaging.BitmapImage(new Uri(iconString, UriKind.RelativeOrAbsolute)),
};
System.Windows.Media.RenderOptions.SetBitmapScalingMode(image, System.Windows.Media.BitmapScalingMode.HighQuality);
return image;
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
}
#endregion
#region Double -> GridLength
public class GridLengthConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var length = value?.ToString();
try
{
return (GridLength)TypeDescriptor.GetConverter(typeof(GridLength)).ConvertFromString(length);
}
catch
{
return new GridLength(1, GridUnitType.Auto);
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
}
#endregion
#region Combiner
public class MultiCombinerConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return values.Clone();
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return new object[] { DependencyProperty.UnsetValue, DependencyProperty.UnsetValue };
}
}
#endregion
#region NotAlignmentCenter
public class NotAlignmentCenterConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is HorizontalAlignment)
{
var alignment = (HorizontalAlignment)value;
return alignment != HorizontalAlignment.Center;
}
else if (value is VerticalAlignment)
{
var alignment = (VerticalAlignment)value;
return alignment != VerticalAlignment.Center;
}
else
return true;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
}
#endregion
}

View File

@@ -0,0 +1,24 @@
using System;
using System.Windows.Data;
namespace AIStudio.Wpf.DiagramDesigner.Converters
{
public class IntToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (object.Equals(value, 0d))
return false;
else
return true;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (object.Equals(value, false))
return 0d;
else
return 1d;
}
}
}

View File

@@ -12,5 +12,6 @@ using System.Windows.Markup;
[assembly: XmlnsDefinition("https://gitee.com/akwkevin/aistudio.-wpf.-diagram", "AIStudio.Wpf.DiagramDesigner")]
[assembly: XmlnsDefinition("https://gitee.com/akwkevin/aistudio.-wpf.-diagram", "AIStudio.Wpf.DiagramDesigner.Controls")]
[assembly: XmlnsDefinition("https://gitee.com/akwkevin/aistudio.-wpf.-diagram", "AIStudio.Wpf.DiagramDesigner.Converters")]
[assembly: XmlnsPrefix("https://gitee.com/akwkevin/aistudio.-wpf.-diagram", "dd")]

View File

@@ -0,0 +1,77 @@
using System;
namespace AIStudio.Wpf.DiagramDesigner.Services
{
/// <summary>
/// Simple service interface
/// </summary>
public interface IServiceProvider
{
IUIVisualizerService VisualizerService { get; }
IMessageBoxService MessageBoxService { get; }
//IDatabaseAccessService DatabaseAccessService { get; }
}
/// <summary>
/// Simple service locator
/// </summary>
public class ServiceProvider : IServiceProvider
{
private IUIVisualizerService visualizerService = new WPFUIVisualizerService();
private IMessageBoxService messageBoxService = new WPFMessageBoxService();
//private IDatabaseAccessService databaseAccessService = new DatabaseAccessService();
public IUIVisualizerService VisualizerService
{
get { return visualizerService; }
}
public IMessageBoxService MessageBoxService
{
get { return messageBoxService; }
}
//public IDatabaseAccessService DatabaseAccessService
//{
// get { return databaseAccessService; }
//}
}
/// <summary>
/// Simple service locator helper
/// </summary>
public class ApplicationServicesProvider
{
private static Lazy<ApplicationServicesProvider> instance = new Lazy<ApplicationServicesProvider>(() => new ApplicationServicesProvider());
private IServiceProvider serviceProvider = new ServiceProvider();
private ApplicationServicesProvider()
{
}
static ApplicationServicesProvider()
{
}
public void SetNewServiceProvider(IServiceProvider provider)
{
serviceProvider = provider;
}
public IServiceProvider Provider
{
get { return serviceProvider; }
}
public static ApplicationServicesProvider Instance
{
get { return instance.Value; }
}
}
}

View File

@@ -0,0 +1,175 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AIStudio.Wpf.DiagramDesigner.Services
{
/// <summary>
/// Available Button options.
/// Abstracted to allow some level of UI Agnosticness
/// </summary>
public enum CustomDialogButtons
{
OK,
OKCancel,
YesNo,
YesNoCancel
}
/// <summary>
/// Available Icon options.
/// Abstracted to allow some level of UI Agnosticness
/// </summary>
public enum CustomDialogIcons
{
None,
Information,
Question,
Exclamation,
Stop,
Warning
}
/// <summary>
/// Available DialogResults options.
/// Abstracted to allow some level of UI Agnosticness
/// </summary>
public enum CustomDialogResults
{
None,
OK,
Cancel,
Yes,
No
}
/// <summary>
/// This interface defines a interface that will allow
/// a ViewModel to show a messagebox
/// </summary>
public interface IMessageBoxService
{
/// <summary>
/// Shows an error message
/// </summary>
/// <param name="message">The error message</param>
void ShowError(string message);
/// <summary>
/// Shows an error message with a custom caption
/// </summary>
/// <param name="message">The error message</param>
/// <param name="caption">The caption</param>
void ShowError(string message, string caption);
/// <summary>
/// Shows an information message
/// </summary>
/// <param name="message">The information message</param>
void ShowInformation(string message);
/// <summary>
/// Shows an information message with a custom caption
/// </summary>
/// <param name="message">The information message</param>
/// <param name="caption">The caption</param>
void ShowInformation(string message, string caption);
/// <summary>
/// Shows an warning message
/// </summary>
/// <param name="message">The warning message</param>
void ShowWarning(string message);
/// <summary>
/// Shows an warning message with a custom caption
/// </summary>
/// <param name="message">The warning message</param>
/// <param name="caption">The caption</param>
void ShowWarning(string message, string caption);
/// <summary>
/// Displays a Yes/No dialog and returns the user input.
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="icon">The icon to be displayed.</param>
/// <returns>User selection.</returns>
CustomDialogResults ShowYesNo(string message, CustomDialogIcons icon);
/// <summary>
/// Displays a Yes/No dialog with a custom caption, and returns the user input.
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="caption">The caption</param>
/// <param name="icon">The icon to be displayed.</param>
/// <returns>User selection.</returns>
CustomDialogResults ShowYesNo(string message, string caption, CustomDialogIcons icon);
/// <summary>
/// Displays a Yes/No/Cancel dialog and returns the user input.
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="icon">The icon to be displayed.</param>
/// <returns>User selection.</returns>
CustomDialogResults ShowYesNoCancel(string message, CustomDialogIcons icon);
/// <summary>
/// Displays a Yes/No dialog with a default button selected, and returns the user input.
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="caption">The caption of the message box window</param>
/// <param name="icon">The icon to be displayed.</param>
/// <param name="defaultResult">Default result for the message box</param>
/// <returns>User selection.</returns>
CustomDialogResults ShowYesNo(string message, string caption, CustomDialogIcons icon, CustomDialogResults defaultResult);
/// <summary>
/// Displays a Yes/No/Cancel dialog with a custom caption and returns the user input.
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="caption">The caption</param>
/// <param name="icon">The icon to be displayed.</param>
/// <returns>User selection.</returns>
CustomDialogResults ShowYesNoCancel(string message, string caption, CustomDialogIcons icon);
/// <summary>
/// Displays a Yes/No/Cancel dialog with a default button selected, and returns the user input.
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="caption">The caption of the message box window</param>
/// <param name="icon">The icon to be displayed.</param>
/// /// <param name="defaultResult">Default result for the message box</param>
/// <returns>User selection.</returns>
CustomDialogResults ShowYesNoCancel(string message, string caption, CustomDialogIcons icon, CustomDialogResults defaultResult);
/// <summary>
/// Displays a OK/Cancel dialog and returns the user input.
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="icon">The icon to be displayed.</param>
/// <returns>User selection.</returns>
CustomDialogResults ShowOkCancel(string message, CustomDialogIcons icon);
/// <summary>
/// Displays a OK/Cancel dialog with a custom caption and returns the user input.
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="caption">The caption</param>
/// <param name="icon">The icon to be displayed.</param>
/// <returns>User selection.</returns>
CustomDialogResults ShowOkCancel(string message, string caption, CustomDialogIcons icon);
/// <summary>
/// Displays a OK/Cancel dialog with a default button selected, and returns the user input.
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="caption">The caption of the message box window</param>
/// <param name="icon">The icon to be displayed.</param>
/// <param name="defaultResult">Default result for the message box</param>
/// <returns>User selection.</returns>
CustomDialogResults ShowOkCancel(string message, string caption, CustomDialogIcons icon, CustomDialogResults defaultResult);
}
}

View File

@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AIStudio.Wpf.DiagramDesigner.Services
{
/// <summary>
/// This interface defines a UI controller which can be used to display dialogs
/// in either modal form from a ViewModel.
/// </summary>
public interface IUIVisualizerService
{
/// <summary>
/// This method displays a modal dialog associated with the given key.
/// </summary>
/// <param name="dataContextForPopup">Object state to associate with the dialog</param>
/// <returns>True/False if UI is displayed.</returns>
bool? ShowDialog(object dataContextForPopup);
}
}

View File

@@ -0,0 +1,392 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace AIStudio.Wpf.DiagramDesigner.Services
{
/// <summary>
/// This class implements the IMessageBoxService for WPF purposes.
/// </summary>
public class WPFMessageBoxService : IMessageBoxService
{
#region IMessageBoxService Members
/// <summary>
/// Displays an error dialog with a given message.
/// </summary>
/// <param name="message">The message to be displayed.</param>
public void ShowError(string message)
{
ShowMessage(message, "Error", CustomDialogIcons.Stop);
}
/// <summary>
/// Displays an error dialog with a given message and caption.
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="caption">The caption of the message box window</param>
public void ShowError(string message, string caption)
{
ShowMessage(message, caption, CustomDialogIcons.Stop);
}
/// <summary>
/// Displays an error dialog with a given message.
/// </summary>
/// <param name="message">The message to be displayed.</param>
public void ShowInformation(string message)
{
ShowMessage(message, "Information", CustomDialogIcons.Information);
}
/// <summary>
/// Displays an error dialog with a given message.
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="caption">The caption of the message box window</param>
public void ShowInformation(string message, string caption)
{
ShowMessage(message, caption, CustomDialogIcons.Information);
}
/// <summary>
/// Displays an error dialog with a given message.
/// </summary>
/// <param name="message">The message to be displayed.</param>
public void ShowWarning(string message)
{
ShowMessage(message, "Warning", CustomDialogIcons.Warning);
}
/// <summary>
/// Displays an error dialog with a given message.
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="caption">The caption of the message box window</param>
public void ShowWarning(string message, string caption)
{
ShowMessage(message, caption, CustomDialogIcons.Warning);
}
/// <summary>
/// Displays a Yes/No dialog and returns the user input.
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="icon">The icon to be displayed.</param>
/// <returns>User selection.</returns>
public CustomDialogResults ShowYesNo(string message, CustomDialogIcons icon)
{
return ShowQuestionWithButton(message, icon, CustomDialogButtons.YesNo);
}
/// <summary>
/// Displays a Yes/No dialog and returns the user input.
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="caption">The caption of the message box window</param>
/// <param name="icon">The icon to be displayed.</param>
/// <returns>User selection.</returns>
public CustomDialogResults ShowYesNo(string message, string caption, CustomDialogIcons icon)
{
return ShowQuestionWithButton(message, caption, icon, CustomDialogButtons.YesNo);
}
/// <summary>
/// Displays a Yes/No dialog with a default button selected, and returns the user input.
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="caption">The caption of the message box window</param>
/// <param name="icon">The icon to be displayed.</param>
/// <param name="defaultResult">Default result for the message box</param>
/// <returns>User selection.</returns>
public CustomDialogResults ShowYesNo(string message, string caption, CustomDialogIcons icon, CustomDialogResults defaultResult)
{
return ShowQuestionWithButton(message, caption, icon, CustomDialogButtons.YesNo, defaultResult);
}
/// <summary>
/// Displays a Yes/No/Cancel dialog and returns the user input.
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="icon">The icon to be displayed.</param>
/// <returns>User selection.</returns>
public CustomDialogResults ShowYesNoCancel(string message, CustomDialogIcons icon)
{
return ShowQuestionWithButton(message, icon, CustomDialogButtons.YesNoCancel);
}
/// <summary>
/// Displays a Yes/No/Cancel dialog and returns the user input.
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="caption">The caption of the message box window</param>
/// <param name="icon">The icon to be displayed.</param>
/// <returns>User selection.</returns>
public CustomDialogResults ShowYesNoCancel(string message, string caption, CustomDialogIcons icon)
{
return ShowQuestionWithButton(message, caption, icon, CustomDialogButtons.YesNoCancel);
}
/// <summary>
/// Displays a Yes/No/Cancel dialog with a default button selected, and returns the user input.
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="caption">The caption of the message box window</param>
/// <param name="icon">The icon to be displayed.</param>
/// <param name="defaultResult">Default result for the message box</param>
/// <returns>User selection.</returns>
public CustomDialogResults ShowYesNoCancel(string message, string caption, CustomDialogIcons icon, CustomDialogResults defaultResult)
{
return ShowQuestionWithButton(message, caption, icon, CustomDialogButtons.YesNoCancel, defaultResult);
}
/// <summary>
/// Displays a OK/Cancel dialog and returns the user input.
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="icon">The icon to be displayed.</param>
/// <returns>User selection.</returns>
public CustomDialogResults ShowOkCancel(string message, CustomDialogIcons icon)
{
return ShowQuestionWithButton(message, icon, CustomDialogButtons.OKCancel);
}
/// <summary>
/// Displays a OK/Cancel dialog and returns the user input.
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="caption">The caption of the message box window</param>
/// <param name="icon">The icon to be displayed.</param>
/// <returns>User selection.</returns>
public CustomDialogResults ShowOkCancel(string message, string caption, CustomDialogIcons icon)
{
return ShowQuestionWithButton(message, caption, icon, CustomDialogButtons.OKCancel);
}
/// <summary>
/// Displays a OK/Cancel dialog with a default result selected, and returns the user input.
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="caption">The caption of the message box window</param>
/// <param name="icon">The icon to be displayed.</param>
/// <param name="defaultResult">Default result for the message box</param>
/// <returns>User selection.</returns>
public CustomDialogResults ShowOkCancel(string message, string caption, CustomDialogIcons icon, CustomDialogResults defaultResult)
{
return ShowQuestionWithButton(message, caption, icon, CustomDialogButtons.OKCancel, defaultResult);
}
#endregion
#region Private Methods
/// <summary>
/// Shows a standard System.Windows.MessageBox using the parameters requested
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="caption">The heading to be displayed</param>
/// <param name="icon">The icon to be displayed.</param>
private void ShowMessage(string message, string caption, CustomDialogIcons icon)
{
MessageBox.Show(message, caption, MessageBoxButton.OK, GetImage(icon));
}
/// <summary>
/// Shows a standard System.Windows.MessageBox using the parameters requested
/// but will return a translated result to enable adhere to the IMessageBoxService
/// implementation required.
///
/// This abstraction allows for different frameworks to use the same ViewModels but supply
/// alternative implementations of core service interfaces
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="icon">The icon to be displayed.</param>
/// <param name="button"></param>
/// <returns>CustomDialogResults results to use</returns>
private CustomDialogResults ShowQuestionWithButton(string message,
CustomDialogIcons icon, CustomDialogButtons button)
{
MessageBoxResult result = MessageBox.Show(message, "Please confirm...",
GetButton(button), GetImage(icon));
return GetResult(result);
}
/// <summary>
/// Shows a standard System.Windows.MessageBox using the parameters requested
/// but will return a translated result to enable adhere to the IMessageBoxService
/// implementation required.
///
/// This abstraction allows for different frameworks to use the same ViewModels but supply
/// alternative implementations of core service interfaces
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="caption">The caption of the message box window</param>
/// <param name="icon">The icon to be displayed.</param>
/// <param name="button"></param>
/// <returns>CustomDialogResults results to use</returns>
private CustomDialogResults ShowQuestionWithButton(string message, string caption,
CustomDialogIcons icon, CustomDialogButtons button)
{
MessageBoxResult result = MessageBox.Show(message, caption,
GetButton(button), GetImage(icon));
return GetResult(result);
}
/// <summary>
/// Shows a standard System.Windows.MessageBox using the parameters requested
/// but will return a translated result to enable adhere to the IMessageBoxService
/// implementation required.
///
/// This abstraction allows for different frameworks to use the same ViewModels but supply
/// alternative implementations of core service interfaces
/// </summary>
/// <param name="message">The message to be displayed.</param>
/// <param name="caption">The caption of the message box window</param>
/// <param name="icon">The icon to be displayed.</param>
/// <param name="button"></param>
/// <param name="defaultResult">Default result for the message box</param>
/// <returns>CustomDialogResults results to use</returns>
private CustomDialogResults ShowQuestionWithButton(string message, string caption,
CustomDialogIcons icon, CustomDialogButtons button, CustomDialogResults defaultResult)
{
MessageBoxResult result = MessageBox.Show(message, caption,
GetButton(button), GetImage(icon), GetResult(defaultResult));
return GetResult(result);
}
/// <summary>
/// Translates a CustomDialogIcons into a standard WPF System.Windows.MessageBox MessageBoxImage.
/// This abstraction allows for different frameworks to use the same ViewModels but supply
/// alternative implementations of core service interfaces
/// </summary>
/// <param name="icon">The icon to be displayed.</param>
/// <returns>A standard WPF System.Windows.MessageBox MessageBoxImage</returns>
private MessageBoxImage GetImage(CustomDialogIcons icon)
{
MessageBoxImage image = MessageBoxImage.None;
switch (icon)
{
case CustomDialogIcons.Information:
image = MessageBoxImage.Information;
break;
case CustomDialogIcons.Question:
image = MessageBoxImage.Question;
break;
case CustomDialogIcons.Exclamation:
image = MessageBoxImage.Exclamation;
break;
case CustomDialogIcons.Stop:
image = MessageBoxImage.Stop;
break;
case CustomDialogIcons.Warning:
image = MessageBoxImage.Warning;
break;
}
return image;
}
/// <summary>
/// Translates a CustomDialogButtons into a standard WPF System.Windows.MessageBox MessageBoxButton.
/// This abstraction allows for different frameworks to use the same ViewModels but supply
/// alternative implementations of core service interfaces
/// </summary>
/// <param name="btn">The button type to be displayed.</param>
/// <returns>A standard WPF System.Windows.MessageBox MessageBoxButton</returns>
private MessageBoxButton GetButton(CustomDialogButtons btn)
{
MessageBoxButton button = MessageBoxButton.OK;
switch (btn)
{
case CustomDialogButtons.OK:
button = MessageBoxButton.OK;
break;
case CustomDialogButtons.OKCancel:
button = MessageBoxButton.OKCancel;
break;
case CustomDialogButtons.YesNo:
button = MessageBoxButton.YesNo;
break;
case CustomDialogButtons.YesNoCancel:
button = MessageBoxButton.YesNoCancel;
break;
}
return button;
}
/// <summary>
/// Translates a standard WPF System.Windows.MessageBox MessageBoxResult into a
/// CustomDialogIcons.
/// This abstraction allows for different frameworks to use the same ViewModels but supply
/// alternative implementations of core service interfaces
/// </summary>
/// <param name="result">The standard WPF System.Windows.MessageBox MessageBoxResult</param>
/// <returns>CustomDialogResults results to use</returns>
private CustomDialogResults GetResult(MessageBoxResult result)
{
CustomDialogResults customDialogResults = CustomDialogResults.None;
switch (result)
{
case MessageBoxResult.Cancel:
customDialogResults = CustomDialogResults.Cancel;
break;
case MessageBoxResult.No:
customDialogResults = CustomDialogResults.No;
break;
case MessageBoxResult.None:
customDialogResults = CustomDialogResults.None;
break;
case MessageBoxResult.OK:
customDialogResults = CustomDialogResults.OK;
break;
case MessageBoxResult.Yes:
customDialogResults = CustomDialogResults.Yes;
break;
}
return customDialogResults;
}
/// <summary>
/// Translates a CustomDialogResults into a standard WPF System.Windows.MessageBox MessageBoxResult
/// This abstraction allows for different frameworks to use the same ViewModels but supply
/// alternative implementations of core service interfaces
/// </summary>
/// <param name="result">The CustomDialogResults</param>
/// <returns>The standard WPF System.Windows.MessageBox MessageBoxResult results to use</returns>
private MessageBoxResult GetResult(CustomDialogResults result)
{
MessageBoxResult customDialogResults = MessageBoxResult.None;
switch (result)
{
case CustomDialogResults.Cancel:
customDialogResults = MessageBoxResult.Cancel;
break;
case CustomDialogResults.No:
customDialogResults = MessageBoxResult.No;
break;
case CustomDialogResults.None:
customDialogResults = MessageBoxResult.None;
break;
case CustomDialogResults.OK:
customDialogResults = MessageBoxResult.OK;
break;
case CustomDialogResults.Yes:
customDialogResults = MessageBoxResult.Yes;
break;
}
return customDialogResults;
}
#endregion
}
}

View File

@@ -0,0 +1,29 @@
using System.Windows;
using AIStudio.Wpf.DiagramDesigner.Controls;
namespace AIStudio.Wpf.DiagramDesigner.Services
{
public class WPFUIVisualizerService : IUIVisualizerService
{
#region Public Methods
/// <summary>
/// This method displays a modal dialog associated with the given key.
/// </summary>
/// <param name="dataContextForPopup">Object state to associate with the dialog</param>
/// <returns>True/False if UI is displayed.</returns>
public bool? ShowDialog(object dataContextForPopup)
{
Window win = new PopupWindow();
win.DataContext = dataContextForPopup;
win.Owner = Application.Current.MainWindow;
if (win != null)
return win.ShowDialog();
return false;
}
#endregion
}
}