簡體   English   中英

綁定文本塊WinRT-XAML上的條件格式

[英]Conditional formatting on binding textblock WinRT-XAML

我在一個月前開始申請,這是我第一次構建移動應用程序並第一次使用XAML,盡管我之前有過使用C#的經驗。

這是我使用的數據格式:

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>

我正在嘗試創建一個搜索結果頁面,該頁面將加粗或更改用戶插入的“查詢”的ForeGround顏色。 我讀了很多文章,發現一個帖子說我們不能從代碼背后改變樣式設置器。

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

我認為這可能更像是這樣:

<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>

(編輯)顯然WINRT-XAML不支持上面的代碼,它是WPF-XAML

但是,如何使用該代碼定位特定單詞? 有什么建議 ?

謝謝。

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

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

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

謝謝你們這里的所有答案。 但最后,我想出了這個:

這是我從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>

這是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);
                }
            }
        }
    }
}

我非常感謝你所有的時間和思想......

這真的對我很有幫助..

謝謝 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM