Consider the following scenario:
I want to bind the TextElement.FontWeight property to an xml attribute. Xml looks something like this and has arbitrary depth.
<text font-weight="bold">
bold text here
<inlinetext>more bold text</inlinetext>
even more bold text
</text>
I use hierarchical templating to display text, without problems, but having a setter in the style of the template, for example:
<Setter Property="TextElement.FontWeight" Value="{Binding XPath=@font-weight}"/>
correctly sets the font size at the first level, but overwrites the second level with a zero value (since the binding cannot find xpath), which returns to normal in the Font.
I tried all kinds of things here, but nothing works.
eg. I used the converter to return UnsetValue, which did not work.
I am currently trying:
<Setter Property="custom:AttributeInserter.Wrapper" Value="{custom:AttributeInserter Property=TextElement.FontWeight, Binding={Binding XPath=@font-weight}}"/>
Codebehind:
public static class AttributeInserter
{
public static AttributeInserterExtension GetWrapper(DependencyObject obj)
{
return (AttributeInserterExtension)obj.GetValue(WrapperProperty);
}
public static void SetWrapper(DependencyObject obj, AttributeInserterExtension value)
{
obj.SetValue(WrapperProperty, value);
}
public static readonly DependencyProperty WrapperProperty =
DependencyProperty.RegisterAttached("Wrapper", typeof(AttributeInserterExtension), typeof(AttributeInserter), new UIPropertyMetadata(pcc));
static void pcc(DependencyObject o,DependencyPropertyChangedEventArgs e)
{
var n=e.NewValue as AttributeInserterExtension;
var c = o as FrameworkElement;
if (n == null || c==null || n.Property==null || n.Binding==null)
return;
var bex = c.SetBinding(n.Property, n.Binding);
bex.UpdateTarget();
if (bex.Status == BindingStatus.UpdateTargetError)
c.ClearValue(n.Property);
}
}
public class AttributeInserterExtension : MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
public DependencyProperty Property { get; set; }
public Binding Binding { get; set; }
}
which works but cannot track property changes
? ?