在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) 所以这是我的TextBlock
:
<TextBlock Text="1 Projects / 1 Issues"></TextBlock>
Run Code Online (Sandbox Code Playgroud)
使用数据绑定我想更换1和2 {Binding Path=OpenProjects}
和{Binding Path=OpenIssues}
.做这个的最好方式是什么?
PS我没有结婚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 …
我在我的视图中有下一个代码:
<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,因此整个触发器正常工作.怎么了?
TextBlock在Silverlight XAML文件中包装文本.
我有一个奇怪而恼人的问题.我有一个文本块,位于水平定向的堆栈面板内.如果我可以将文本块显示在包含很多包装的多行中,那就太好了.我怎么能这样做?
今天我用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)
我想念什么?
我有一个带有文本块和图像的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)
我如何将我的文本块对齐到中心..?
正如您在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
,我想HorizontalScrollBar
becames可见.
我试图处理SizeChanged
事件,并根据条件设置ScrollViewer.HorizontalScrollBarVisibility
附加属性.但是当我将后者Disabled
改为时,文本就不会被修剪Visible
- 所以它以某种方式跳转 - 从修剪到完全,这不是最好的UI练习.
问:那么,如何实现上述行为?
我的问题与此类似: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 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) textblock ×10
wpf ×8
xaml ×6
c# ×2
data-binding ×2
styles ×2
datatrigger ×1
listbox ×1
mvvm ×1
scrollviewer ×1
silverlight ×1
text ×1
texttrimming ×1
winrt-xaml ×1