TextBlock에서 텍스트 서식 지정
TextBlock
내 WPF 응용 프로그램 의 컨트롤 내에서 텍스트 서식을 어떻게 지정 합니까?
예 : 다음 예와 같이 특정 단어는 굵게, 다른 단어는 기울임 꼴로, 일부는 다른 색상으로 표시하고 싶습니다.
내 질문의 이유는 다음과 같은 실제 문제입니다.
lblcolorfrom.Content = "Colour From: " + colourChange.ElementAt(3).Value.ToUpper();
문자열의 두 번째 부분을 굵게 표시하고 싶습니다. 두 개의 컨트롤 (라벨, TextBlocks 등)을 사용할 수 있다는 것을 알고 있지만 이미 사용중인 방대한 양의 컨트롤로 인해 사용하지 않는 것이 좋습니다.
다음을 사용해야합니다 Inlines
.
<TextBlock.Inlines>
<Run FontWeight="Bold" FontSize="14" Text="This is WPF TextBlock Example. " />
<Run FontStyle="Italic" Foreground="Red" Text="This is red text. " />
</TextBlock.Inlines>
바인딩 사용 :
<TextBlock.Inlines>
<Run FontWeight="Bold" FontSize="14" Text="{Binding BoldText}" />
<Run FontStyle="Italic" Foreground="Red" Text="{Binding ItalicText}" />
</TextBlock.Inlines>
다른 속성을 바인딩 할 수도 있습니다.
<TextBlock.Inlines>
<Run FontWeight="{Binding Weight}"
FontSize="{Binding Size}"
Text="{Binding LineOne}" />
<Run FontStyle="{Binding Style}"
Foreground="Binding Colour}"
Text="{Binding LineTwo}" />
</TextBlock.Inlines>
부울로 굵게 표시하면 변환기를 통해 바인딩 할 수 있습니다 (예 :).
XAML에서 쉽게이 작업을 수행 할 수 있습니다.
<TextBlock>
Hello <Bold>my</Bold> faithful <Underline>computer</Underline>.<Italic>You rock!</Italic>
</TextBlock>
이 다양 Inline
사용할 수있는 간단한 서식 옵션에 대한 당신을 도울 수있는 요소는 Bold
, Italic
및 Underline
:
<TextBlock>
Sample text with <Bold>bold</Bold>, <Italic>italic</Italic> and <Underline>underlined</Underline> words.
</TextBlock>
이 요소는 사실 Span
다양한 속성이 설정된 요소에 대한 속기 일 뿐이라는 점에 주목할 가치가 있습니다 (예 : for Bold
, FontWeight
속성이로 설정 됨 FontWeights.Bold
).
그러면 다음 옵션 인 앞서 언급 한 Span
요소가 나타납니다.
이 요소로 위와 같은 효과를 얻을 수 있지만 더 많은 가능성이 부여됩니다. Foreground
또는 Background
속성을 설정할 수 있습니다.
<TextBlock>
Sample text with <Span FontWeight="Bold">bold</Span>, <Span FontStyle="Italic">italic</Span> and <Span TextDecorations="Underline">underlined</Span> words. <Span Foreground="Blue">Coloring</Span> <Span Foreground="Red">is</Span> <Span Background="Cyan">also</Span> <Span Foreground="Silver">possible</Span>.
</TextBlock>
Span
요소는이 같은 다른 요소를 포함 할 수 있습니다 :
<TextBlock>
<Span FontStyle="Italic">Italic <Span Background="Yellow">text</Span> with some <Span Foreground="Blue">coloring</Span>.</Span>
</TextBlock>
아주 유사하다 다른 요소,가 Span
,가 호출됩니다 Run
. 는 Run
그동안 다른 인라인 요소를 포함 할 수 없습니다 Span
캔,하지만 당신은 쉽게 할 수있는 바인드받는 변수 Run
의 Text
특성 :
<TextBlock>
Username: <Run FontWeight="Bold" Text="{Binding UserName}"/>
</TextBlock>
Also, you can do the whole formatting from code-behind if you prefer:
TextBlock tb = new TextBlock();
tb.Inlines.Add("Sample text with ");
tb.Inlines.Add(new Run("bold") { FontWeight = FontWeights.Bold });
tb.Inlines.Add(", ");
tb.Inlines.Add(new Run("italic ") { FontStyle = FontStyles.Italic });
tb.Inlines.Add("and ");
tb.Inlines.Add(new Run("underlined") { TextDecorations = TextDecorations.Underline });
tb.Inlines.Add("words.");
Check out this example from Charles Petzolds Bool Application = Code + markup
//----------------------------------------------
// FormatTheText.cs (c) 2006 by Charles Petzold
//----------------------------------------------
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Documents;
namespace Petzold.FormatTheText
{
class FormatTheText : Window
{
[STAThread]
public static void Main()
{
Application app = new Application();
app.Run(new FormatTheText());
}
public FormatTheText()
{
Title = "Format the Text";
TextBlock txt = new TextBlock();
txt.FontSize = 32; // 24 points
txt.Inlines.Add("This is some ");
txt.Inlines.Add(new Italic(new Run("italic")));
txt.Inlines.Add(" text, and this is some ");
txt.Inlines.Add(new Bold(new Run("bold")));
txt.Inlines.Add(" text, and let's cap it off with some ");
txt.Inlines.Add(new Bold(new Italic (new Run("bold italic"))));
txt.Inlines.Add(" text.");
txt.TextWrapping = TextWrapping.Wrap;
Content = txt;
}
}
}
a good site, with good explanations:
http://www.wpf-tutorial.com/basic-controls/the-textblock-control-inline-formatting/
here the author gives you good examples for what you are looking for! Overal the site is great for research material plus it covers a great deal of options you have in WPF
Edit
There are different methods to format the text. for a basic formatting (the easiest in my opinion):
<TextBlock Margin="10" TextWrapping="Wrap">
TextBlock with <Bold>bold</Bold>, <Italic>italic</Italic> and <Underline>underlined</Underline> text.
</TextBlock>
Example 1 shows basic formatting with Bold Itallic and underscored text.
Following includes the SPAN method, with this you van highlight text:
<TextBlock Margin="10" TextWrapping="Wrap">
This <Span FontWeight="Bold">is</Span> a
<Span Background="Silver" Foreground="Maroon">TextBlock</Span>
with <Span TextDecorations="Underline">several</Span>
<Span FontStyle="Italic">Span</Span> elements,
<Span Foreground="Blue">
using a <Bold>variety</Bold> of <Italic>styles</Italic>
</Span>.
</TextBlock>
Example 2 shows the span function and the different possibilities with it.
For a detailed explanation check the site!
이것은 내 해결책입니다 ....
<TextBlock TextWrapping="Wrap" Style="{DynamicResource InstructionStyle}">
<Run Text="This wizard will take you through the purge process in the correct order." FontWeight="Bold"></Run>
<LineBreak></LineBreak>
<Run Text="To Begin, select" FontStyle="Italic"></Run>
<Run x:Name="InstructionSection" Text="'REPLACED AT RUNTIME'" FontWeight="Bold"></Run>
<Run Text="from the menu." FontStyle="Italic"></Run>
</TextBlock>
나는 배우는 중입니다 ... 위의 해결책에 대해 누군가가 생각한다면 공유하십시오! :)
참고 URL : https://stackoverflow.com/questions/5263055/formatting-text-in-a-textblock
'Programing' 카테고리의 다른 글
다중 사용자 Ajax 웹 애플리케이션을 동시에 안전하게 설계하는 방법 (0) | 2020.08.28 |
---|---|
cURL을 통해 파일을 저장할 때 특정 파일 이름을 지정하는 방법이 있습니까? (0) | 2020.08.28 |
활동 시작시 ScrollView에서 맨 아래로 스크롤하는 방법 (0) | 2020.08.27 |
"x"라는 이름의 경로가 이미 경로 모음에 있습니다. (0) | 2020.08.27 |
Android 에뮬레이터에서 localhost를 어떻게 연결합니까? (0) | 2020.08.27 |