Change XML Style Text in Windows Store App

I am creating an application in which I have a table of contents. I created an XML file to extract data from it. In my XML, I want to change the style of some text, however I could not do it from the XML code. I tried to put the tag <b>, <bold>, <strong>, but they did not work

The following is the XML code:

<book>
    <item type="Module">
      <title>My family and I</title>
    </item>
    <item type="Unit">
      <title>World Friends</title>
    </item>
    <item type="Unit">
      <title>Sport and activities</title>
    </item>
    <item type="Module">
      <title>
        <b>School days</b></title>
    </item>
    <item type="Unit">
      <title>My routine</title>
    </item>
    <item type="Unit">
      <title>School life</title>
    </item>
</book>

this is my xml. As you can see, I have 2 types: Moduleand Unit. I want to make the text in bold in the type module and indent the text in the Unit type.

I also created a class to call the header. I will show you the codes below:

This is my class

public class ContentTable
{
    string itemTitle;
    public string ItemTitle
    {
        get { return itemTitle; }
        set { itemTitle = value; }
    }
}

This is the code to retrieve data.

string XMLPath = Path.Combine(
   Package.Current.InstalledLocation.Path, "Assets/tableOfContent.xml");
XDocument loadedData = XDocument.Load(XMLPath);

//retrieving data from xml using LINQ     
var data = from query in loadedData.Descendants("item")
    select new ContentTable
    {
        ItemTitle = (string)query.Element("title")
    };

//assigning source to GridView Control     
AllItemsView.ItemsSource = data;

this is my xaml

<ListBox x:Name="AllItemsView" Width="200" Margin="45,20,5,-604" Height="665" VerticalAlignment="Top" Foreground="Black" Background="White" Grid.RowSpan="2" SelectionChanged="AllItemsView_SelectionChanged">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Margin="10" >
                <TextBlock Text="{Binding ItemTitle}"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
+4
source share
1

WPF: Style TextBlock, DataTrigger TextBlock . , ( ) WinRT. , IValueConverter, . , , .

, , , , , . , , , , ContentTable:

public class ContentTable
{
    public string ItemTitle { get; set; }
    public string ItemType { get; set; }
}

ItemTitle ItemType, type XML.

, , ItemTitle:

var data = from query in loadedData.Descendants("item")
           select new ContentTable
           {
               ItemTitle = query.Element("title").Value,
               ItemType = query.Attribute("type").Value
           };

TextBlock (, FontWeight Margin), , string type TextBlock:

class ItemTypeToBoldConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        string text = value as string;

        if (text != null)
        {
            return text == "Module" ?
                Windows.UI.Text.FontWeights.Bold :
                Windows.UI.Text.FontWeights.Normal;
        }

        return Windows.UI.Xaml.DependencyProperty.UnsetValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

class ItemTypeToMarginConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        string text = value as string;

        if (text != null)
        {
            return text == "Unit" ?
                new Windows.UI.Xaml.Thickness(20, 0, 0, 0) :
                new Windows.UI.Xaml.Thickness();
        }

        return Windows.UI.Xaml.DependencyProperty.UnsetValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

, .:)

(, DataTemplate):

<Page.Resources>
  <local:ItemTypeToBoldConverter x:Key="itemTypeToBoldConverter1"/>
  <local:ItemTypeToMarginConverter x:Key="itemTypeToMarginConverter1"/>
</Page.Resources>

, :

<DataTemplate>
  <StackPanel Margin="10" >
    <TextBlock Text="{Binding ItemTitle}"
                FontWeight="{Binding ItemType, Converter={StaticResource itemTypeToBoldConverter1}}"
                Margin="{Binding ItemType, Converter={StaticResource itemTypeToMarginConverter1}}"/>
  </StackPanel>
</DataTemplate>

, FontWeight Margin, , ItemType. , . ItemType Module, ; . , ItemType Unit, ( 20); 0.


, XML XAML UI. HTML, <b>, <bold> <strong>. , . HTML. , XAML. , HTML-, .

, IMHO XSL, XML HTML-, XsltTransform ( , WinRT & hellip; ). WebView ( WPF, WebBrowser) HTML.

+3

All Articles