2013-05-20 37 views
3

我在一個月前開始了我的應用程序,這是我第一次構建移動應用程序,並且第一次使用XAML,即使我以前有過使用C#的經驗。關於綁定textblock的條件格式WinRT-XAML

這是我使用的數據格式:

idAyat namaKitab abbKitab numBab numAyat isi 
    1  kejadian  kej  1   1  some long string to process blah blah 
    2  kejadian  kej  1   2  some long string to process blah blah 
    3  kejadian  kej  1   3  some long string to process query blah 
    4  kejadian  kej  1   4  some long string to process blah query 
    5  kejadian  kej  1   5  some query string to process blah blah 

這是我的XAML代碼:

<GridView x:Name="gvResult"> 
    <GridView.ItemsPanel> 
     <ItemsPanelTemplate> 
      <local:WrapPanel 
       Orientation="Vertical"/> 
     </ItemsPanelTemplate> 
    </GridView.ItemsPanel> 
    <GridView.ItemTemplate> 
     <DataTemplate> 
      <Grid Margin="5"> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="auto"/> 
        <RowDefinition Height="auto"/> 
       </Grid.RowDefinitions> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="300" /> 
       </Grid.ColumnDefinitions> 
       <TextBlock Width="300" TextWrapping="Wrap"> 
        <Underline> 
         <Run FontWeight="Medium" Text="{Binding abbKitab}"/><Run Text=" "/><Run FontWeight="Medium" Text="{Binding numBab}"/> 
         <Run FontWeight="Medium" Text=":"/> <Run FontWeight="Medium" Text="{Binding numAyat}"/> 
        </Underline> 
        <LineBreak/> 
        <Run Text="{Binding isi}"/> 
       </TextBlock> 
      </Grid> 
     </DataTemplate> 
    </GridView.ItemTemplate> 
</GridView> 

我試圖創建一個搜索結果頁面即會大膽或更改前景色用戶插入的「查詢」。 我讀過很多文章,發現one線程表示我們無法從後面的代碼中更改樣式設置器。

假設文章是正確的,如何更改我的頁面中我的文本塊的前景色? 更具體地說,我只是想改變匹配搜索查詢的單詞的顏色。

我認爲這將是更多像這樣也許:

<Style x:Key="PriorityStyle" TargetType="TextBlock" > 
    <Setter Property="Foreground" Value="#6c6d6f" /> 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding Priority}" Value="Critical"> 
      <Setter Property="Foreground" Value="Red"/> 
     </DataTrigger> 
    </Style.Triggers> 
</Style> 

(EDIT)顯然,以上驗證碼不被WinRT中,XAML的支持,這是WPF的XAML

但我怎麼指定特定的那個代碼的單詞?有什麼建議麼 ?

謝謝。

+0

我沒有看到你的數據優先級,但假設它存在我的TextBlock的前景屬性綁定到優先級和使用價值轉換器把它變成刷子。 – Denis

+0

你是對的,該塊代碼是從這[鏈接](http://stackoverflow.com/a/15624365/2297716) –

+0

如果你看到上面的更新部分,這是爲WPF,我不知道如何我在WINRT-XAML上做到這一點.. 我想弄清楚,如何更改與用戶SearchQuery匹配的「isi」列的某些文本的Foreground屬性,在這種情況下,上面的數據中的「查詢」字。 我試過處理搜索過程後列,增加標籤到選定的字,顯然是沒有工作過,不能轉換到樣式值,它成爲字符串值.. –

回答

2

謝謝你們所有的答案在這裏。但最後,我想出了這個:

這是從SearchResultPage.xaml我的XAML代碼:

<GridView x:Name="gvResult"> 
     <GridView.ItemsPanel> 
      <ItemsPanelTemplate> 
       <local:WrapPanel2 
        Orientation="Vertical"/> 
      </ItemsPanelTemplate> 
     </GridView.ItemsPanel> 
     <GridView.ItemTemplate> 
      <DataTemplate> 
       <local:SearchResultUC/> 
      </DataTemplate> 
     </GridView.ItemTemplate> 
    </GridView> 

,這是我SearchResultUC:

<UserControl 
    x:Class="BibleApps.SearchResultUC" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:BibleApps" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    d:DesignHeight="300" 
    d:DesignWidth="400"> 

    <Grid Margin="5"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="auto"/> 
      <RowDefinition Height="auto"/> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="300" /> 
     </Grid.ColumnDefinitions> 
     <TextBlock Width="300" Grid.Row="0" > 
       <Underline> 
        <Run FontWeight="Medium" Text="{Binding abbKitab}"/><Run Text=" "/><Run FontWeight="Medium" Text="{Binding numBab}"/> 
        <Run FontWeight="Medium" Text=":"/> <Run FontWeight="Medium" Text="{Binding numAyat}"/> 
       </Underline> 
     </TextBlock> 
     <TextBlock TextWrapping="Wrap" Grid.Row="1" local:FormattedTextBehavior.FormattedText="{Binding isi}"/> 
    </Grid> 
</UserControl> 

,這是從ANSWER FormattedTextBehavior.cs:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.IO; 
using Windows.Foundation; 
using Windows.Foundation.Collections; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Controls.Primitives; 
using Windows.UI.Xaml.Data; 
using Windows.UI.Xaml.Input; 
using Windows.UI.Xaml.Documents; 
using Windows.UI.Xaml.Media; 
using Windows.UI.Xaml.Navigation; 
using BibleApps.Common; 
using BibleApps.DataModel; 

namespace BibleApps 
{ 
    public class FormattedTextBehavior : DependencyObject 
    { 
     public static string GetFormattedText(DependencyObject obj) 
     { 
      return (string)obj.GetValue(FormattedTextProperty); 
     } 

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

     public static readonly DependencyProperty FormattedTextProperty = 
      DependencyProperty.RegisterAttached("FormattedText", 
               typeof(string), 
               typeof(FormattedTextBehavior), 
               new PropertyMetadata("", FormattedTextChanged)); 

     private static void FormattedTextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) 
     { 
      TextBlock textBlock = sender as TextBlock; 
      string value = e.NewValue as string; 
      string[] tokens = value.Split(' '); 
      string[] querytokens = SuspensionManager.SessionState["query"].ToString().Split(' '); 
      foreach (string token in tokens) 
      { 
       Run kata = new Run(); 
       bool ketemu = false; 
       foreach (string querytoken in querytokens) 
       { 
        if (token.ToLower().Contains(querytoken.ToLower())) { 
         ketemu = true; 
         break; 
        } 
       } 
       if (ketemu){ 
        kata.Foreground = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 0, 255, 80)); 
        kata.FontWeight = Windows.UI.Text.FontWeights.Bold; 
        kata.Text = token + " "; 
        textBlock.Inlines.Add(kata); 
       } 
       else { 
        kata.Text = token + " "; 
        textBlock.Inlines.Add(kata); 
       } 
      } 
     } 
    } 
} 

我感謝大家的所有時間和你的想法..

真的,真的幫助我..

謝謝:)

2

由於WPF觸發器未在WinRT中實現,因此可以在GridView上定義DataTemplateSelector。

在此TemplateSelector中,定義兩個模板,一個用於「常規」條目,另一個用於「搜索」條目。

在模板選擇器的SelectTemplate方法中,只需測試數據對象的屬性,以檢查是否必須應用一個或另一個模板。

+0

THX你的迴應:) –

+2

一簡單正確的解決方案。您可以通過http://diptimayapatra.wordpress.com/2012/07/24/data-template-selector-in-windows-8-metro-xaml-app/瞭解DataTemplateSelectors。其實很簡單。 –