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