开发者

Why Can't You Bind To TextDecoration In Silverlight?

I have 2 TextBlocks in my Silverlight .XAML definition. I want the second TextBlock to emulate the first TextBlock's behavior. The simplest way I found to do this is via element databinding. This is all well and good, but I get the following error when I try to bind to the TextDecoration property:

Unable to cast object of type 'System.Wi开发者_StackOverflow社区ndows.Data.Binding' to type 'System.Windows.TextDecorationCollection'.

For reference, my XAML looks like this:

<TextBlock x:Name="TextBlock1" Text="Booya" />
<TextBlock x:Name="TextBlock2" Text="AnotherBooya" FontSize="{Binding FontSize, ElementName=TextBlock1}" FontFamily="{Binding FontFamily, ElementName=TextBlock1}" TextDecorations="{Binding TextDecorations, ElementName=TextBlock1}"/>

If I remove the TextDecorations="{Binding TextDecorations, ElementName=txt_FanName}" part, then the code compiles just fine. Is this because the TextDecoration property can accept a value of Null while others (I.e.: FontWeight) can't?

My Question is: Why is this the case? And are there any work arounds (aside from manually setting this in the code-behind)?


This appears to be a bug in the Designer. At runtime the binding works fine. However its a pretty diblitating bug while designing because design are becomes useless.

One work-around I found is to create an "Identity" converter, that is a converter that simply returns the input value unmodified:-

public class IdentityConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value;
    }
}

Have one of these in the local Resources node and use it on the binding. The exception doesn't go away immediately but having run the app and returned to design the designer seems happy enough to render but the blue swiggly line under the binding remains.

Once your happy the whole Xaml works so you don't need to do further design you can remove this kludge.


I've added a binding for TextDecorations in the initialization code (e.g. constructor) for my Control in which the TextBlock resides as follows:

//The following are because of bug in the XAML designer
Binding  binding = new Binding("fontUnderln"); // fontUnderln is a property in my DataContext
txtBlockDlgImgTiltle.SetBinding(TextBlock.TextDecorationsProperty, binding);
txtBlockDlgImgText.SetBinding(TextBlock.TextDecorationsProperty, binding);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜