Hi,
Could you provide a code snippet of changing the color of a link in the “Button_Click” event. When I see how you do this I should be able to change the other link properties.
Thanks
Rich
Hi,
Could you provide a code snippet of changing the color of a link in the “Button_Click” event. When I see how you do this I should be able to change the other link properties.
Thanks
Rich
Throughout XAML-oriented programming there is always this tension between making programmatic changes “directly” and making changes indirectly and depending on data-binding.
Choice #1:
[code] private void Button_Click(object sender, System.Windows.RoutedEventArgs e) {
// find the Node that contains the Button
Node node = Part.FindAncestor(e.OriginalSource as UIElement);
if (node == null) return;
// Programmatically toggling the colors of links connected to this node
Brush brush = new SolidColorBrush(Colors.Orange);
Brush black = new SolidColorBrush(Colors.Black);
foreach (Link link in node.LinksConnected) {
Shape linkshape = link.FindNamedDescendant("Shape") as Shape;
if (linkshape != null) {
SolidColorBrush br = linkshape.Stroke as SolidColorBrush;
if (br != null && br.Color == Colors.Black)
linkshape.Stroke = brush;
else
linkshape.Stroke = black;
}
}
}[/code]
Choice #2:
[code] private void Button_Click(object sender, System.Windows.RoutedEventArgs e) {
// find the Node that contains the Button
Node node = Part.FindAncestor(e.OriginalSource as UIElement);
if (node == null) return;
// Toggling connected link colors via data-binding
foreach (Link link in node.LinksConnected) {
Relationship rel = link.Data as Relationship;
if (rel != null) rel.Color = (rel.Color == "Black" ? "Orange" : "Black");
}
}[/code]
Choice #2 also requires adding a Color property to the Relationship data class, which I did by just copy-and-paste from the Entity example class and also setting the initial value to “Black”.
And choice #2 requires changing the DataTemplate for links. Instead of setting Stroke=“Black” for the link shape path:
<Path go:LinkPanel.IsLinkShape="True" x:Name="Shape"
Stroke="Black"
StrokeThickness="1.5" />
Bind it to the Relationship.Color property:
<Path go:LinkPanel.IsLinkShape="True" x:Name="Shape"
Stroke="{Binding Path=Data.Color, Converter={StaticResource theStringBrushConverter}}"
StrokeThickness="1.5" />
Which choice is best for you depends on the nature of the operation.
Direct changes are sometimes easier to understand and implement.
Changes via data-binding are more tolerant of change, for example when there is a change to the template, or when there are multiple elements that want to use the same color.
Actually, there’s a better choice #2A – declaring the new Relationship.Color property to be a boolean, and defining the two colors in a BooleanBrushConverter in the XAML rather than in the C# code.