2014-04-19 85 views
0

當ContentPresenter的content屬性是String類型時,它會自動使用TextBlock作爲其子元素。但是我需要應用程序中的所有ContentPresenter使用稱爲DynamicTextBlock(用於CharacterTrimming的Silverlight中衆所周知的用戶控件)而不是默認的TextBlock控件。更改ContentPresenter中的默認文本塊

我該如何做到這樣?

回答

0

讓我們面對它。微軟已經證實Silverlight是死的。如果可能的話,你應該跳到WPF。不過,讓我試着回答你的問題。 wpf中有一些非常酷的功能,它的被調用的屬性值繼承。讓我告訴你如何:

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; 
     } 
    } 
} 

這是WPF,你可以看到。我不知道這對你有多大幫助,但你去了哪裏。我認爲Silverlight也有這個屬性值繼承。

現在使用習慣:

<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> 

或者這樣:

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

基本上你可以設置你的附加屬性的answere和屬性將被繼承到所有元素。如果該元素是TextBlock,則將設置CharacterElipsis。

1

嗨,我是一名WPF程序員。您請檢查此解決方案是否適用於Silverlight,請嘗試並告訴我。 在下面的代碼中代替TextBlock(在DataTemplate中)使用您的silverlight文本塊名稱,並讓我知道結果。

<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> 
+0

+1 cool answer。 –

+0

謝謝@HeenaPatil – Sivasubramanian