Hi,
Connectors of some items are generated dynamically in run-time, so I tried to bind Connectors using Extension. Drag-drop process are definitely working good, but when I bind all diagram,before calling extension method, it throws exception "XmlParseException - The default connectors cannot be removed because connections are attached to it.".
Thanks in advance.
Connectors of some items are generated dynamically in run-time, so I tried to bind Connectors using Extension. Drag-drop process are definitely working good, but when I bind all diagram,before calling extension method, it throws exception "XmlParseException - The default connectors cannot be removed because connections are attached to it.".
Thanks in advance.
public
class
ConnectorExtension
{
public
static
readonly
DependencyProperty ConnectorsProperty =
DependencyProperty.RegisterAttached(
"Connectors"
,
typeof
(List<ConnectorModel>),
typeof
(ConnectorExtension),
new
UIPropertyMetadata(
null
,
new
PropertyChangedCallback(ConnectorsChanged)));
public
static
void
SetConnectors(UIElement element, List<ConnectorModel> value)
{
element.SetValue(ConnectorsProperty, value);
}
public
static
List<ConnectorModel> GetConnectors(UIElement element)
{
return
(List<ConnectorModel>)element.GetValue(ConnectorsProperty);
}
public
static
void
ConnectorsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var connectors = e.NewValue
as
List<ConnectorModel>;
var shape = d
as
RadDiagramShapeBase;
if
(shape !=
null
)
{
if
(connectors !=
null
)
{
shape.UseDefaultConnectors =
false
;
shape.Connectors.Clear();
connectors.ForEach(connector => shape.Connectors.Add(
new
RadDiagramConnector() { Offset = connector.Offset, Name = connector.Name, Tag=connector.Name }));
}
else
{
shape.UseDefaultConnectors =
false
;
shape.Connectors.Clear();
}
}
}
}
}
<
Style
x:Key
=
"DynamicConnectorStyle"
TargetType
=
"telerik:RadDiagramShape"
>
<
Setter
Property
=
"Position"
Value
=
"{Binding Position, Mode=TwoWay}"
/>
<
Setter
Property
=
"IsSelected"
Value
=
"{Binding IsSelected}"
/>
<
Setter
Property
=
"Height"
Value
=
"{Binding Height}"
/>
<
Setter
Property
=
"Width"
Value
=
"{Binding Width}"
/>
<
Setter
Property
=
"Background"
Value
=
"Transparent"
/>
<
Setter
Property
=
"IsEditable"
Value
=
"False"
/>
<
Setter
Property
=
"StrokeThickness"
Value
=
"0"
/>
<
Setter
Property
=
"UseDefaultConnectors"
Value
=
"False"
/>
<
Setter
Property
=
"extensions:ConnectorExtension.Connectors"
Value
=
"{Binding Path=Connectors,UpdateSourceTrigger=PropertyChanged}"
/>
<
Setter
Property
=
"ContentTemplate"
Value
=
"{Binding Path=TemplateKey,Converter={StaticResource DataTemplateSelector}}"
/>
</
Style
>