标签: textblock

如何让TextBlock逐字地说出"{Binding}"?

在XAML中,如果插入

<TextBlock Text="Hello World" />
Run Code Online (Sandbox Code Playgroud)

你会看到"Hello World"这个词.

如果你插入

<TextBlock Text="{Binding}" />
Run Code Online (Sandbox Code Playgroud)

它将触发数据绑定功能.但是如果我真的希望显示文字是"{Binding}"呢?"

XAML字符串中是否存在等效的转义字符?

或者是我唯一的解决方案:

<TextBlock>Binding</TextBlock>
Run Code Online (Sandbox Code Playgroud)

data-binding wpf xaml text textblock

3
推荐指数
2
解决办法
371
查看次数

WPF:TextBlock中的部分数据绑定

所以这是我的TextBlock:

<TextBlock Text="1 Projects / 1 Issues"></TextBlock>
Run Code Online (Sandbox Code Playgroud)

使用数据绑定我想更换1和2 {Binding Path=OpenProjects}{Binding Path=OpenIssues}.做这个的最好方式是什么?

PS我没有结婚TextBlock.

data-binding wpf textblock

3
推荐指数
1
解决办法
855
查看次数

使用单一样式设置Hyperlink和TextBlock的样式?

我有两种类型的文本需要遵循基于枚举的类似着色规则:

 public enum Modes
 {
   A,
   B,
   C
 }
Run Code Online (Sandbox Code Playgroud)

DataTrigger标记的样式用于着色:

      <Style TargetType="SEE BELOW" x:Key="Coloring">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=.}" Value="A">
                    <Setter Property="Foreground" Value="Red" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=.}" Value="B">
                    <Setter Property="Foreground" Value="Green" />
                </DataTrigger>
               <DataTrigger Binding="{Binding Path=.}" Value="C">
                    <Setter Property="Foreground" Value="Blue" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
Run Code Online (Sandbox Code Playgroud)

一个使用场景是System.Windows.Documents.Hyperlink嵌套的System.Windows.Controls.TextBlock:

<Hyperlink><TextBlock/></Hyperlink>
Run Code Online (Sandbox Code Playgroud)

另一个很简单TextBlock:

<TextBlock Style="{StaticResource Coloring}" Text="yada"/>
Run Code Online (Sandbox Code Playgroud)

当然,我可以设计两个TextBlock元素:

<TextBlock Style="{StaticResource Coloring}" Text="yada"/>
<Hyperlink><TextBlock Style="{StaticResource Coloring}"/></Hyperlink>
Run Code Online (Sandbox Code Playgroud)

但这无法确定Hyperlink案例的下划线风格.

如果我尝试两种类型的样式:

<TextBlock Style="{StaticResource Coloring}" Text="yada"/>
<Hyperlink Style="{StaticResource Coloring}"><TextBlock/></Hyperlink>
Run Code Online (Sandbox Code Playgroud)

然后样式失败,因为(显然)没有共同的祖先类型用于TargetType …

wpf xaml styles textblock

3
推荐指数
1
解决办法
5561
查看次数

在触发器中更改TextBlock.Text不起作用

我在我的视图中有下一个代码:

<Style x:Key="documentFileNameStyle">
    <Setter Property="TextBlock.Foreground" Value="Gray"/>
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=Untitled}" Value="True">
            <Setter Property="TextBlock.FontStyle" Value="Italic"/>
            <Setter Property="TextBlock.Text" Value="no file name"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

<DataTemplate x:Key="documentTemplate">            
    <TextBlock Text="{Binding Path=FileName}" Style="{StaticResource documentFileNameStyle}"/>                                
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

但是将TextBlock.Text设置为字符串不起作用.TextBlock.FontStyle更改为Italic,因此整个触发器正常工作.怎么了?

wpf styles datatrigger textblock

3
推荐指数
1
解决办法
4358
查看次数

TextBlock在Silverlight XAML文件中包装文本

TextBlock在Silverlight XAML文件中包装文本.

我有一个奇怪而恼人的问题.我有一个文本块,位于水平定向的堆栈面板内.如果我可以将文本块显示在包含很多包装的多行中,那就太好了.我怎么能这样做?

c# silverlight textblock silverlight-4.0

3
推荐指数
1
解决办法
2952
查看次数

WPF如何在底线使用不同的字体大小排列TextBlock

今天我用wpf遇到了一个非常令人讨厌的问题.

我只想在底线对齐Textblock控件(使用不同的fontsize).

        <StackPanel Grid.Row="0" Orientation="Horizontal">
            <TextBlock Text="ABC" FontSize="12" VerticalAlignment="Bottom"/>
            <TextBlock Text="QWERT" FontSize="24" VerticalAlignment="Bottom"/>
            <TextBlock Text="XYZ" FontSize="18" VerticalAlignment="Bottom"/>
        </StackPanel>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我想念什么?

wpf xaml textblock

3
推荐指数
1
解决办法
5166
查看次数

XAML - 旋转后文本块的居中对齐方式

我有一个带有文本块和图像的stackpanel.我需要将文本块对齐到旋转中心(90度).就像这样,

在此输入图像描述

但是,在旋转文本块后,我总是得到这样的结果,

在此输入图像描述

这是我正在使用的XAML代码,

<StackPanel Orientation="Horizontal">
        <Image Width="120" Source="ms-appx:///Assets/mail.jpg"/>
        <TextBlock Text="send mail" FontSize="15" Margin="25,0,0,0" >
            <TextBlock.RenderTransform>
                <RotateTransform Angle="90"/>
            </TextBlock.RenderTransform>
        </TextBlock>
    </StackPanel>
Run Code Online (Sandbox Code Playgroud)

我如何将我的文本块对齐到中心..?

xaml textblock xaml-designer winrt-xaml

3
推荐指数
1
解决办法
3919
查看次数

TextBox中的文本未在ListBox内正确修剪

正如您在stackoverflow帖子中看到的那样,当ListBox的大小变小时,此代码不会修剪TextBlock的文本.

 <ListBox ItemsSource="{Binding}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" TextTrimming="CharacterEllipsis"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)

列表框显示了HorizontalScrollBar-换句话说,房间的TextBlock足够大并且在文字修剪没有必要.这可以使用下一行代码解决:

<ListBox ...
         ScrollViewer.HorizontalScrollBarVisibility="Disabled"
         ... />
Run Code Online (Sandbox Code Playgroud)

所以现在文本将被修剪.但是,如果我不想修剪整个文本,直到唯一...会留下来?让说,我设置MinWidth了的TextBlock的财产,并在宽度列表框 becames小于MinWidth,我想HorizontalScrollBarbecames可见.

我试图处理SizeChanged事件,并根据条件设置ScrollViewer.HorizontalScrollBarVisibility附加属性.但是当我将后者Disabled改为时,文本就不会被修剪Visible- 所以它以某种方式跳转 - 从修剪到完全,这不是最好的UI练习.

:那么,如何实现上述行为?

wpf listbox textblock scrollviewer texttrimming

3
推荐指数
1
解决办法
2699
查看次数

将文本绑定到附加属性

我的问题与此类似:WPF生成TextBlock内联,但我没有足够的声誉来评论.这是附加的属性类:

public class Attached
{
    public static readonly DependencyProperty FormattedTextProperty = DependencyProperty.RegisterAttached(
        "FormattedText",
        typeof(string),
        typeof(TextBlock),
        new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.AffectsMeasure));

    public static void SetFormattedText(DependencyObject textBlock, string value)
    {
        textBlock.SetValue(FormattedTextProperty, value);
    }

    public static string GetFormattedText(DependencyObject textBlock)
    {
        return (string)textBlock.GetValue(FormattedTextProperty);
    }

    private static void FormattedTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var textBlock = d as TextBlock;
        if (textBlock == null)
        {
            return;
        }

        var formattedText = (string)e.NewValue ?? string.Empty;
        formattedText = string.Format("<Span xml:space=\"preserve\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">{0}</Span>", formattedText);

        textBlock.Inlines.Clear();
        using (var xmlReader = XmlReader.Create(new StringReader(formattedText))) …
Run Code Online (Sandbox Code Playgroud)

wpf xaml textblock mvvm attached-properties

3
推荐指数
1
解决办法
2891
查看次数

突出显示WPF TextBlock中的文本

我试图突出显示或设置WPF TextBlock中某些选定文本的背景.假设我有2个文本文件,我加载到内存中,完成差异,然后想要在WPF应用程序中显示.想象一下,循环遍历每一行,然后将文本附加到文本块并根据已删除,插入或相等的文本更改颜色.

for (int i = 0; i < theDiffs.Count; i++)
        {
            switch (theDiffs[i].operation)
            {
                case Operation.DELETE:
                    // set color to red on Source control version TextBlock
                    break;

                case Operation.INSERT:
                    WorkspaceVersion.AppendText(theDiffs[i].text);
                    // set the background color (or highlight) of appended text to green
                    break;

                case Operation.EQUAL:
                    WorkspaceVersion.AppendText(theDiffs[i].text);
                    // Set the background color (highlight) of appended text to yellow
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml textblock

3
推荐指数
1
解决办法
3498
查看次数