我需要在Silverlight中动态创建带有连接组件的类似UML的框图。这该怎么做?应该使用什么控件?需要为块中的某些项添加显示/隐藏切换。
答案 0 :(得分:2)
您可以将ItemsControl与Canvas一起使用。
namespace SlBug1
{
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
public partial class UML : UserControl
{
public UML()
{
InitializeComponent();
SetupData();
this.DataContext = this;
}
public object Classes { get; set; }
public object Arrows { get; set; }
#region Set up data
public void SetupData()
{
var arrows = new List<Arrow>(
new Arrow[] {
new Arrow(50, 250, 200, 250),
new Arrow(50, 50, 200, 220)
}
);
this.Arrows = arrows;
var classes = new List<Class>(
new Class[]
{
new Class { Name = "Line", Methods= new string[] { "Draw", "setColor"} , X=10, Y=11, IsExpanded=true},
new Class { Name = "Rectangle", Methods = new string[] { "Draw", "setColor", "setFill"}, X=200, Y=200, IsExpanded=true},
new Class { Name = "Circle", Methods = new string[] {"Draw", "setColor", "setFill", "setRadius"}, X=10, Y=200, IsExpanded=false},
});
this.Classes = classes;
}
#endregion
}
public class Class
{
public string Name { get; set; }
public IEnumerable<string> Methods { get; set; }
public double X { get; set; }
public double Y { get; set; }
public bool IsExpanded { get; set; }
}
public class Arrow
{
public Arrow(double X1, double Y1, double X2, double Y2)
{
this.X1 = X1; this.Y1 = Y1;
this.X2 = X2; this.Y2 = Y2;
}
public double X1 { get; set; }
public double Y1 { get; set; }
public double X2 { get; set; }
public double Y2 { get; set; }
public double LX1 { get { return (14 * X2 + X1) / 15; } }
public double RX1 { get { return (14 * X2 + X1) / 15; } }
public double LY1 { get { return (14 * Y2 + Y1) / 15 - 10; } }
public double RY1 { get { return (14 * Y2 + Y1) / 15 + 10; } }
}
}
<UserControl x:Class="SlBug1.UML"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<Canvas>
<ItemsControl x:Name="ArrowsCanvas" ItemsSource="{Binding Arrows}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Canvas>
<Line X1="{Binding X1}" X2="{Binding X2}"
Y1="{Binding Y1}" Y2="{Binding Y2}" Stroke="Black" />
<Line X1="{Binding LX1}" X2="{Binding X2}"
Y1="{Binding LY1}" Y2="{Binding Y2}" Stroke="Black" />
<Line X1="{Binding RX1}" X2="{Binding X2}"
Y1="{Binding RY1}" Y2="{Binding Y2}" Stroke="Black" />
</Canvas>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<ItemsControl x:Name="UmlCanvas" ItemsSource="{Binding Classes}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<!-- Hack: http://forums.silverlight.net/post/370493.aspx -->
<Canvas>
<Border Background="LightBlue"
Canvas.Left="{Binding X}"
Canvas.Top="{Binding Y}"
BorderBrush="Black" BorderThickness="2" >
<StackPanel>
<TextBlock Text="{Binding Name}" FontSize="16" />
<ItemsControl
ItemsSource="{Binding Methods}"
Margin="10,10,0,0"/>
</StackPanel>
</Border>
</Canvas>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Canvas>
</Grid>
</UserControl>
最终结果如下所示:
答案 1 :(得分:1)
在此处查看此帖:C# .NET Document Based Application manipulating Diagrams
它讨论了java / WPF图表选项。我认为我在该文章中建议的链接( WPF Diagram Designer - Part 4 )是一个非常强大的竞争者,可以让您开始。祝你好运。
答案 2 :(得分:1)
我发现GoXam是一个非常好的产品。它并不便宜,但它构造得非常好,能够很好地完成这项任务。它还包括内置的布局包,因此您的图表可以看起来很漂亮。
编辑:有30天的试用期。在线查看ER Diagram样本 - 它可能非常接近你想要的。
答案 3 :(得分:0)
为什么不只是css + javascript?如果我没弄错的话,你可以在silverlight中生成/嵌入html。
答案 4 :(得分:0)
尝试使用MiniUML。这个客户端WPF,但我认为你可以适应自己
答案 5 :(得分:0)
我们广泛使用MindFusion中的DiagramLite来进行此类应用。它工作得非常好,而且价格合理。
答案 6 :(得分:0)