2014-10-18 60 views
1

我有一個字符串屬性,將顯示爲我的應用程序的磁貼內容。我想要做以下事情:在這個字符串的第一行中,我想以默認字體大小顯示一些基本信息,但是在這個字符串的第二行中,我想顯示兩個字體大小較大的值...仍然這個字符串屬性是一個...有沒有什麼辦法來實現這個? 在此先感謝!Windows手機:更改相同字符串內的字體大小

回答

0

<RichTextBox在WP8恕我直言,有點難對付。但是,只需一個<TextBlock>和一個轉換器就可以輕鬆完成。

基本上使用相同的字符串運行,並將其傳遞給你的轉換器,它返回你想格式化的子字符串(在你的情況下,你的字符串爲你的行號)。

<!-- MyString is your Property, MyConverter is your Converter you programmed, ConverterParamter is the row_number that you want to pass to MyConver --> 
<TextBlock x:Name="tb"> 
    <Run FontSize="12" Text="{Binding MyString, Converter={StaticResource MyConverter}, ConverterParameter=0}"/> 
    <Run FontSize="24" Text="{Binding MyString, Converter={StaticResource MyConverter}, ConverterParameter=1}"/>     
</TextBlock> 

// sample Converter of what you want to do 
using System.Windows.Data; 
public class MyConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     string return_string = ""; 
     int rowid = (int) parameter; 
     switch (rowid) 
     { 
      case 0: 
       return_string = "sub_string_row_0"; // calculate the substring for row 0 
       break; 
      case 1: 
       return_string = "sub_string_row_1"; // calculate the substring for row 1 
       break; 
      default: 
       break; 
     }  
     return return_string; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     return null; 
    } 
} 

記得來定義轉換器在你的資源,像這樣

<phone:PhoneApplicationPage.Resources> 
    <converter:MyConverter x:Key="MyConverter"/> 
</phone:PhoneApplicationPage.Resources> 

哪裏<conveter:>

<phone:PhoneApplicationPage 
    xmlns:converter="clr-namespace:YOUR_NAMESPACE_OF_YOUR_PROGRAM"> 
+0

非常感謝你的回答......我想我明白你給我建議的方式,但我從XAML對象獲取文本,並通過它的部分粘在C#中的字符串...我會得到'tb'文本塊的文本? – 2014-10-19 19:44:22

+0

@AlexAllafi哦,你不直接設置TB的文本。我將它的值綁定到名爲「MyString」的屬性。如果您想要一個示例,請在Databinding上查找示例教程(這非常簡單)。 – 2014-10-21 00:23:38

0

如果你創建3個額外的屬性,它們將返回你的「主」字符串的一部分,這將會容易得多。

例如:

public string PartOne { get { return myString.SubString(0, 3); }}