c# - 在 Graphsharp 中向边缘添加自定义样式

标签 c# wpf xaml graph-sharp

我正在使用文档很少的 GraphSharp 框架 (http://graphsharp.codeplex.com/),并且我正在尝试更改某些边缘的颜色。

实际上是这样的(使这条边变红)。

g.AddEdge(new Edge<object>("A","B"), Color.Red);

有人有这方面的代码片段吗?

最佳答案

这里是解决方案。您应该创建一个自定义边缘类型,并将颜色信息存储在边缘对象本身中,或者能够创建一个可以从边缘对象计算颜色的转换器。

此外,您应该使用一些Style 自定义EdgeControl

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.Navigation;
using System.Windows.Shapes;
using GraphSharp;
using QuickGraph;
using GraphSharp.Controls;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public IBidirectionalGraph<object, IEdge<object>> Graph { get; set; }

        public MainWindow()
        {
            var g = new BidirectionalGraph<object, IEdge<object>>();

            IList<Object> vertices = new List<Object>();
            for (int i = 0; i < 6; i++)
            {
                vertices.Add(i.ToString() );
            }

            for (int i = 0; i < 5; i++)
            {
                Color edgeColor = (i % 2 == 0) ? Colors.Black : Colors.Red;
                Console.WriteLine(edgeColor);

                g.AddVerticesAndEdge(new MyEdge(vertices[i], vertices[i+1]) { 
                    Id = i.ToString(),
                    EdgeColor = edgeColor
                });
            }

            Graph = g;

            Console.WriteLine(Graph.VertexCount);
            Console.WriteLine(Graph.EdgeCount);

            InitializeComponent();
        }
    }

    public class MyEdge : TypedEdge<Object>
    {
        public String Id { get; set; }

        public Color EdgeColor { get; set; }

        public MyEdge(Object source, Object target) : base(source, target, EdgeTypes.General) { }
    }

    public class EdgeColorConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return new SolidColorBrush((Color) value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:graphsharp="clr-namespace:GraphSharp.Controls;assembly=GraphSharp.Controls"
        xmlns:my="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525"
        Name="root">
    <Grid>
        <Grid.Resources>
            <my:EdgeColorConverter x:Key="edgeToEdgeColorConverter"/>
            <Style TargetType="{x:Type graphsharp:EdgeControl}">
                <Style.Setters>
                    <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource Self},Path=Edge.EdgeColor,Converter={StaticResource edgeToEdgeColorConverter}}"/>
                </Style.Setters>
            </Style>
        </Grid.Resources>

        <graphsharp:GraphLayout x:Name="graphLayout" 
                                Graph="{Binding ElementName=root,Path=Graph}" 
                                OverlapRemovalAlgorithmType="FSA"
                                HighlightAlgorithmType="Simple"
                                LayoutAlgorithmType="FR"/>
    </Grid>
</Window>

关于c# - 在 Graphsharp 中向边缘添加自定义样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12935835/

相关文章:

c# - 用户定义表类型 (UDTT) 参数是否可以与其他类型混合

c# - WPF C# 输入框

c# - 如何将 IsEnabled 绑定(bind)到 xaml 中的 vm 属性以获取 DataTemplate 中的按钮?

c# - 如何从代码后面打开日期选择器的日历?

wpf - ContentControl 崩溃中的绑定(bind)

c# - WPF - 从 StackPanel 中删除 "User Control"子级

c# - 如何在 WPF MVVM 中将 View 的变量绑定(bind)到 ViewModel?

c# - 是否可以在 'RightToLeft' 属性设置为 'Yes' 的组合框中使用左对齐文本?

javascript - 表单提交后如何停止从部分页面重定向

c# - 为什么 List<T> 属性的通知不起作用