Harsha
Harsha

Reputation: 113

Changing default textblock in ContentPresenter

When the content property of a ContentPresenter is of String type, it automatically uses a TextBlock as its child element. But I need all ContentPresenters in my application to use something called a DynamicTextBlock (a user control well known in Silverlight for CharacterTrimming) instead of the default TextBlock control.

How do I achieve something like this?

Upvotes: 0

Views: 1481

Answers (2)

Sivasubramanian
Sivasubramanian

Reputation: 955

Hi I am a WPF programmer. You please check whether this solution works in Silverlight, please give it a try and let me know. In the below code in place of TextBlock (inside DataTemplate) use your silverlight textblock name, and let me know the results.

<Window x:Class="Editable_ComboBox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>

        <DataTemplate DataType="{x:Type sys:String}" >
            <TextBlock Text="{Binding}" TextTrimming="CharacterEllipsis" Background="AliceBlue" Foreground="Red" />
            <!--In the above line, Remove the TextBlock and use your silvelight dynamic textblock name-->
        </DataTemplate>

    </Window.Resources>
    <Grid>
        <Button Content="Button" HorizontalAlignment="Left" Margin="204,146,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>
</Window>

Upvotes: 2

dev hedgehog
dev hedgehog

Reputation: 8791

Lets face it. Microsoft already confirmed Silverlight is deadish. You should skip to WPF if anyhow possible. However let me try answer your question. There is something pretty cool in wpf and its called property value inheritance. Let me show you how:

public class MyWpfExtension
{
    public static bool GetCharacterEllipsis(DependencyObject obj)
    {
        return (bool)obj.GetValue(CharacterEllipsisProperty);
    }

    public static void SetCharacterEllipsis(DependencyObject obj, bool value)
    {
        obj.SetValue(CharacterEllipsisProperty, value);
    }

    // Using a DependencyProperty as the backing store for CharacterEllipsis.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CharacterEllipsisProperty =
        DependencyProperty.RegisterAttached("CharacterEllipsis", typeof(bool), typeof(MyWpfExtension),
        new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits, OnCharacterEllipsisChanged));

    private static void OnCharacterEllipsisChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is TextBlock && (bool)e.NewValue)
        {
            TextBlock tb = (TextBlock)d;
            tb.TextTrimming = TextTrimming.CharacterEllipsis;
        }
    }
}

Btw this is WPF like you can see. I dont know how much will this help you but there you go. I assume Silverlight has this property value inheritance as well.

And now to usage:

<StackPanel Background="Blue" local:MyWpfExtension.CharacterEllipsis ="True">
    <TextBlock>asdfsadfsadfsadfasdfsadfsadfsadfasdfsadfsadfsadf</TextBlock>
    <TextBlock>asdfsadfsadfsadfasdfsadfsadfsadfasdfsadfsadfsadf</TextBlock>
    <TextBlock>asdfsadfsadfsadfasdfsadfsadfsadfasdfsadfsadfsadf</TextBlock>
    <TextBlock>asdfsadfsadfsadfasdfsadfsadfsadfasdfsadfsadfsadf</TextBlock>
    <TextBlock>asdfsadfsadfsadfasdfsadfsadfsadfasdfsadfsadfsadf</TextBlock>
    <TextBlock>asdfsadfsadfsadfasdfsadfsadfsadfasdfsadfsadfsadf</TextBlock>
    <TextBlock>asdfsadfsadfsadfasdfsadfsadfsadf</TextBlock>
    <TextBlock>asdfsadfsadfsadfasdfsadfsadfsadf</TextBlock>
</StackPanel>

Or like this:

<StackPanel Background="Blue" >
    <ContentPresenter local:MyWpfExtension.CharacterEllipsis="True" Content="fasdfasdfsdffasdfasdfsdffasdfasdfsdffasdfasdfsdf"></ContentPresenter>
</StackPanel>

Basically you could set your attached property answere and the property will be inherited to all elements. If the element is a TextBlock the CharacterElipsis will be set.

Upvotes: 0

Related Questions