首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >绑定文本块WinRT-XAML上的条件格式

绑定文本块WinRT-XAML上的条件格式
EN

Stack Overflow用户
提问于 2013-05-20 22:56:56
回答 2查看 2.7K关注 0票数 3

我一个月前开始使用我的应用程序,这是我第一次构建移动应用程序,也是第一次使用XAML,尽管我以前有过一些使用C#的经验。

这是我使用的数据格式:

代码语言:javascript
复制
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代码:

代码语言:javascript
复制
<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颜色的“查询”,用户插入。我读了很多文章,发现one线程说我们不能改变代码背后的样式设置器。

假设文章是正确的,如何在页面中更改文本块的前景颜色?更具体地说,我只想更改匹配搜索查询的单词的颜色。

我想可能会更像这样:

代码语言:javascript
复制
<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

但是,如何使用该代码来定位特定的单词呢?有什么建议吗?

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-05-23 19:48:31

感谢你们在这里的所有答案。但最后,我想出了这个:

这是我的SearchResultPage.xaml中的XAML代码:

代码语言:javascript
复制
   <GridView x:Name="gvResult">
        <GridView.ItemsPanel>
            <ItemsPanelTemplate>
                <local:WrapPanel2
                    Orientation="Vertical"/>
            </ItemsPanelTemplate>
        </GridView.ItemsPanel>
        <GridView.ItemTemplate>
            <DataTemplate>
                <local:SearchResultUC/>
            </DataTemplate>
        </GridView.ItemTemplate>
    </GridView>

这是我的SearchResultUC:

代码语言:javascript
复制
<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的答案:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2013-05-23 17:26:43

由于WPF触发器不是在WinRT中实现的,因此您可以在GridView上定义DataTemplateSelector。

在此TemplateSelector中,定义两个模板,一个用于“常规”条目,另一个用于“搜索”条目。

在模板选择器的SelectTemplate方法中,只需测试数据对象的属性,以检查是否必须应用一个或另一个模板。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16652324

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档