【问题标题】:Changing default textblock in ContentPresenter更改 ContentPresenter 中的默认文本块
【发布时间】:2014-04-19 05:52:06
【问题描述】:

当 ContentPresenter 的 content 属性是 String 类型时,它会自动使用 TextBlock 作为其子元素。但我需要我的应用程序中的所有 ContentPresenter 使用称为 DynamicTextBlock 的东西(Silverlight 中众所周知的用于 CharacterTrimming 的用户控件)而不是默认的 TextBlock 控件。

我如何实现这样的目标?

【问题讨论】:

    标签: wpf mvvm silverlight-5.0 controltemplate contentpresenter


    【解决方案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>
    

    【讨论】:

      【解决方案2】:

      让我们面对现实吧。微软已经确认 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。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-28
        • 2011-06-30
        • 1970-01-01
        • 2013-07-16
        • 2013-02-25
        • 1970-01-01
        相关资源
        最近更新 更多