其他分享
首页 > 其他分享> > 绑定文本块WinRT-XAML上的条件格式

绑定文本块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颜色.
我读过很多文章,发现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>

(编辑)显然,WINRT-XAML不支持上面的代码,它是WPF-XAML

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

谢谢.

解决方法:

由于WPF触发器未在WinRT中实现,因此您可以在GridView上定义一个DataTemplateSelector.

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

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

标签:winrt-xaml,textblock,windows-8,microsoft-metro,c
来源: https://codeday.me/bug/20191030/1971293.html