How to change the color of dynamically created lin

Hi

How to change the color of dynamically created link? Binding is not working. I want to change the color in event handler.
Not working example:
<Path go:LinkPanel.IsLinkShape="True"x:Name="path"Stroke="{Binding Path=Data.Color,Converter={StaticResource theColorConverter}, Mode=OneWay}"StrokeThickness="1" />
private void Path_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e) {
here i add some new links
foreach (Link link in myDiagram.Links){
((RelationLinkData)link.Data).Color = Colors.Yellow;
}

What does your ColorConverter do?
Are there any trace messages in the Visual Studio Output window?
Does the link path get the right color for links that you don’t create in your event handler?
Where is that event handler declared?

On a different but not directly related topic, don’t forget to surround your data-modifying code in calls to StartTransaction and CommitTransaction.

The Paths DataBinding is called only one time when the path is initialized. But i havent any data in this time so I want to change the color later.

For example:
First I create some links:
myDiagram.Model.AddLink(childData, null, nodeData, null);
Next i want to change the color of them:
foreach (Link link in myDiagram.Links)
((RelationLinkData)link.Data).Color = Colors.Red;
I want to do this in the Path_MouseLeftButtonUp method (link creating and color changing). But its not working.

To demonstrate this, I made a couple of changes to the LinkDemo sample. (I did this in Silverlight, but it ought to work in WPF too, in case you are using WPF.)

First, in order to support changing the color dynamically, I changed the definition of the Color property of MyLinkData to be:

public String Color { get { return _Color; } set { if (_Color != value) { String old = _Color; _Color = value; RaisePropertyChanged("Color", old, value); } } } private String _Color = "Black";
I also made sure the model was modifiable:

      myDiagram.Model.Modifiable = true;

Then I changed the Path element in the LinkTemplate:

<Path go:LinkPanel.IsLinkShape="True" x:Name="Path" Stroke="{Binding Path=Data.Color}" StrokeThickness="{Binding Path=Data.Thickness, Mode=OneTime}" MouseLeftButtonUp="Path_MouseLeftButtonUp"/>
And here’s the event handler:

[code] private void Path_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e) {
myDiagram.StartTransaction(“add and recolor links”);
MyNodeData n1 = myDiagram.Model.FindNodeByKey(“A”) as MyNodeData;
MyNodeData n2 = myDiagram.Model.FindNodeByKey(“B”) as MyNodeData;
MyLinkData l1 = myDiagram.Model.AddLink(n1, null, n2, null) as MyLinkData;

  Random rand = new Random();
  foreach (Link link in myDiagram.Links) {
    MyLinkData l = link.Data as MyLinkData;
    l.Color = String.Format("#{0:X}{1:X}{2:X}", 120+rand.Next(100), 120+rand.Next(100), 120+rand.Next(100));
  }
  myDiagram.CommitTransaction("add and recolor links");
}[/code]

This all worked well.

I do caution against defining a MouseLeftButtonUp event handler, though, particularly one which modifies so much. Not that you can’t make it work – it’s just too likely to cause accidental changes that the user wouldn’t be expecting.

I tried to modify the Incremental Tree Example, but is not workitng. I cant change the color of links.

My XAML:
<UserControl x:Class="IncrementalTree.IncrementalTree" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:go="clr-namespace:Northwoods.GoXam;assembly=Northwoods.GoSilverlight" xmlns:golayout="clr-namespace:Northwoods.GoXam.Layout;assembly=Northwoods.GoSilverlight"> <UserControl.Resources> <go:StringBrushConverter x:Key="theBrushConverter" /> <go:BooleanStringConverter x:Key="theButtonConverter" TrueString="-" FalseString="+" /> <DataTemplate x:Key="NodeTemplate"> <StackPanel Orientation="Horizontal" go:Part.SelectionAdorned="True" go:Node.IsTreeExpanded="False"> <go:NodePanel Sizing="Auto"> <Path go:NodePanel.Figure="Ellipse" Fill="{Binding Path=Data.Color, Converter={StaticResource theBrushConverter}}" /> <TextBlock Text="{Binding Path=Data.Color}" /> </go:NodePanel> <Button x:Name="myCollapseExpandButton" Click="CollapseExpandButton_Click" Content="{Binding Path=Node.IsExpandedTree, Converter={StaticResource theButtonConverter}}" Width="20" /> </StackPanel> </DataTemplate> <DataTemplate x:Key="LinkTemplate"> <go:LinkPanel go:Part.Visible="True" >

<Path go:LinkPanel.IsLinkShape=“True”
x:Name=“path”
Stroke="{Binding Path=Data.Color, Converter={StaticResource theBrushConverter}}"
StrokeThickness=“1”
/>
<Polygon Points=“8 4 0 8 2 4 0 0”
Fill=“Black”
go:LinkPanel.Alignment=“1 0.5” go:LinkPanel.Index="-1"
go:LinkPanel.Orientation=“Along” />

</go:LinkPanel>
</DataTemplate>
</UserControl.Resources>
<Grid>
<go:Diagram x:Name=“myDiagram” Padding=“10”
InitialPanelSpot=“Center”
HorizontalContentAlignment=“Stretch” VerticalContentAlignment=“Stretch”
NodeTemplate="{StaticResource NodeTemplate}" LinkTemplate="{StaticResource LinkTemplate}">

<go:Diagram.Layout>
<golayout:ForceDirectedLayout ConditionFlags=“Standard VisibleChanged” />
</go:Diagram.Layout>
</go:Diagram>
</Grid>
</UserControl>

My source:
/* Copyright Northwoods Software Corporation, 2008-2009. All Rights Reserved. */ using System; using System.Collections.ObjectModel; using System.Linq; using System.Windows; using System.Windows.Controls; using Northwoods.GoXam; using Northwoods.GoXam.Model; namespace IncrementalTree { public partial class IncrementalTree : UserControl { public IncrementalTree() { InitializeComponent(); // the model is a tree of SimpleData, indexed by strings var model = new GraphLinksModel<SimpleData, String, String, RelationLinkData>(); model.NodeKeyPath = "Key";

// create the root node
model.NodesSource = new ObservableCollection<SimpleData>() {
new SimpleData() { Key="/", Color=“White” }
};
model.LinksSource = new ObservableCollection<RelationLinkData>() { };
model.Modifiable = true;
myDiagram.Model = model;
}
private void CollapseExpandButton_Click(object sender, RoutedEventArgs e)
{
// the Button is in the visual tree of a Node
Button button = (Button)sender;
Node n = Part.FindAncestor<Node>(button);
if (n != null)
{
SimpleData parentdata = (SimpleData)n.Data;
// always make changes within a transaction
myDiagram.StartTransaction(“CollapseExpand”);
// if needed, create the child data for this node
if (!parentdata.EverExpanded)
{
parentdata.EverExpanded = true; // only create children once per node!
int numchildren = CreateSubTree(parentdata);
if (numchildren == 0)
{ // now known no children: don’t need Button!
button.Visibility = Visibility.Collapsed;
}
}
// toggle whether this node is expanded or collapsed
n.IsExpandedTree = !n.IsExpandedTree;

foreach (Link link in myDiagram.Links)
{
((RelationLinkData)link.Data).Color = String.Format("#{0:X}{1:X}{2:X}",
120 + rand.Next(100), 120 + rand.Next(100), 120 + rand.Next(100));
}
myDiagram.CommitTransaction(“CollapseExpand”);
}
}
private int CreateSubTree(SimpleData parentdata)
{
int numchildren = rand.Next(10);
if (myDiagram.PartManager.NodesCount <= 1)
{
numchildren += 1; // make sure the root node has at least one child
}
// create several SimpleData objects and add them to the model
for (int i = 0; i < numchildren; i++)
{
SimpleData childdata = new SimpleData();
childdata.Color = String.Format("#{0:X}{1:X}{2:X}",
120 + rand.Next(100), 120 + rand.Next(100), 120 + rand.Next(100));
childdata.Key = parentdata.Key + “/” + childdata.Color;
childdata.ParentKey = parentdata.Key;
myDiagram.Model.AddNode(childdata);
myDiagram.Model.AddLink(parentdata, null, childdata, null);
}
return numchildren;
}
Random rand = new Random();
}

// Add properties indicating whether we need to find/create child nodes
// and what color the node should be.
// Because these properties are only set at initialization,
// their setters do not need to call RaisePropertyChanged.
public class SimpleData : TreeModelNodeData<String>
{
public bool EverExpanded { get; set; }
public String Color { get; set; }
}
public class RelationLinkData : GraphLinksModelLinkData<string, string>
{
public RelationLinkData()
: base()
{
}
public String Color { get; set; }
}
}

I haven’t tried your code, but it’s immediately obvious that there is a problem with your definitions of the Color property: they don’t implement property change notification.

With a regular CLR property setter, the property field changes value, but that’s it. Nothing else in the system will know that it changed value, so nothing else can react to the setting.

Please follow the first step I listed in my first reply for each of the properties you expect to modify dynamically.