First, salemt, I assume you are using Silverlight.
You want the brush to be dependent on properties of two different objects: your LinkData.Category and the Link.IsSelected properties. The problem is that what you want is MultiBinding, but Silverlight
doesn’t support that.
The normal but convoluted solution for this is to combine the two properties into one property on one object, somehow. Since it doesn’t make sense to put the transient Part.IsSelected property on your LinkData class, we’ll do it the other way around: we’ll add a property to Link that represents both Link.IsSelected and LinkData.Category.
[code] public class CustomLink : Link {
public static readonly DependencyProperty IsSelectedColorProperty =
DependencyProperty.Register(“IsSelectedColor”, typeof(String), typeof(CustomLink), new PropertyMetadata(""));
public String IsSelectedColor {
get { return (String)GetValue(IsSelectedColorProperty); }
set { SetValue(IsSelectedColorProperty, value); }
}
protected override void OnIsSelectedChanged() {
MyLinkData data = this.Data as MyLinkData;
if (data != null) {
if (this.IsSelected)
this.IsSelectedColor = "!" + data.Color;
else
this.IsSelectedColor = data.Color;
}
base.OnIsSelectedChanged();
}
}[/code]
Note how when the link is selected, the property value is the regular color string prefixed with an exclamation mark. Of course you may want to do a completely different way of combining the two property values.
Now how do we get the PartManager to create CustomLink objects instead of the normal Link objects?
public class CustomPartManager : PartManager {
protected override Link MakeLinkForData(object linkdata, IDiagramModel model, DataTemplate templ, string category) {
CustomLink link = new CustomLink();
MyLinkData d = linkdata as MyLinkData;
if (d != null) link.IsSelectedColor = d.Color;
PartBinding data = new PartBinding(link, linkdata);
link.Content = data;
link.DataContext = data;
link.ContentTemplate = templ;
if (category != null && category != "") link.Category = category;
return link;
}
}
This is the regular definition for PartManager.MakeLinkForData, but with it creating a CustomLink instead of the regular Link, and it initializes the value of the new IsSelectedColor property.
We also need to install this CustomPartManager as the Diagram’s PartManager at initialization time:
myDiagram.PartManager = new CustomPartManager();
OK, so now we’re ready to define a String to Brush converter. The following one just produces a lighter color brush if the string starts with “!”.
public class CustomStringBrushConverter : StringBrushConverter {
public override object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
String s = value as String;
if (s != null && s.Length > 1 && s[0] == '!') {
Color c = (Color)myStringColorConverter.Convert(s.Substring(1), typeof(Color), parameter, culture);
c.R = (byte)((c.R+255)/2);
c.G = (byte)((c.G+255)/2);
c.B = (byte)((c.B+255)/2);
return new SolidColorBrush(c);
}
return base.Convert(value, targetType, parameter, culture);
}
private StringColorConverter myStringColorConverter = new StringColorConverter();
}
Of course you’ll need to figure out what you really want to return instead of this contrived example.
Note also that since Silverlight doesn’t support a programmer callable conversion from string to color, you need to use the one that GoXam provides.
Then we can create a resource that is a CustomStringBrushConverter:
<local:CustomStringBrushConverter x:Key="theCustomStringBrushConverter" />
And use it in your DataTemplate(s):
<DataTemplate x:Key="LinkTemplate">
<go:LinkPanel go:Part.SelectionElementName="Path" go:Part.SelectionAdorned="True">
<Path x:Name="Path" go:LinkPanel.IsLinkShape="True"
Stroke="{Binding Path=Link.IsSelectedColor, Converter={StaticResource theCustomStringBrushConverter}}"
StrokeThickness="1" />
<Polygon Fill="Black" Points="8 4 0 8 2 4 0 0"
go:LinkPanel.Alignment="1 0.5" go:LinkPanel.Index="-1" go:LinkPanel.Orientation="Along" />
</go:LinkPanel>
</DataTemplate>
So that’s quite a bit of custom code, just because Silverlight doesn’t support MultiBinding.