2

DatTriggerアイテムのテキストの色を設定する sがいくつかありListViewます。TextBoxとして使用するとItemTemplateListView完全に機能します。でも使ったらダメTextBlock

このコード:

<Style TargetType="TextBox">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Level,Mode=OneWay}"
                     Value="{x:Static Common:LoggingLevel.Error}">
            <Setter Property="Foreground"
                    Value="Red" />
        </DataTrigger>
        <DataTrigger Binding="{Binding Level,Mode=OneWay}"
                     Value="{x:Static Common:LoggingLevel.Warning}">
            <Setter Property="Foreground"
                    Value="Orange" />
        </DataTrigger>
    </Style.Triggers>
</Style>

...

<ListView ItemsSource="{Binding Entries}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBox Text="{Binding Text,Mode=OneWay}" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

適切に色付けされたメッセージのリストを生成します。

このコードは次のとおりです。

<Style TargetType="TextBlock">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Level,Mode=OneWay}"
                     Value="{x:Static Common:LoggingLevel.Error}">
            <Setter Property="Foreground"
                    Value="Red" />
        </DataTrigger>
        <DataTrigger Binding="{Binding Level,Mode=OneWay}"
                     Value="{x:Static Common:LoggingLevel.Warning}">
            <Setter Property="Foreground"
                    Value="Orange" />
        </DataTrigger>
    </Style.Triggers>
</Style>

...

<ListView ItemsSource="{Binding Entries}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Text,Mode=OneWay}" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

デフォルトの黒の前景色ですべてのメッセージをレンダリングします。

4

2 に答える 2

4

暗黙的なスタイルは、テンプレート内で継承する要素に適用されます。直接継承するSystem.Windows.Controls.Controlため、適用されません。それを機能させるには、明示的にスタイルを指定して割り当て、UPDATE内でスタイルを定義する必要があります。トピックをカバーするために、 allに暗黙的に適用する方法があることにも言及する必要があります。入れるとアプリ全体に適用されます。この場合、いくつかの主要なパフォーマンスの問題や潜在的なその他のエラーが発生する可能性があります。Microsoft がこれらの基本要素を、複雑で暗黙的なスタイルを誤って使用しないように保護することにしたのはおそらくそのためです。誰もが気づいているわけではありませんTextBlockFrameworkElementStyle x:KeyTextBlockTextBlock


StyleTextBlocksApplication.ResourcesTextBlocksWindowとして終了しますTextBlock

于 2013-06-08T21:08:34.220 に答える
0

にキーを提供し、以下に示すようStyleに で適用します。TextBlock

<Style TargetType="TextBlock" x:Key="txtID">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Level,Mode=OneWay}" Value="{x:Static Common:LoggingLevel.Error}">
            <Setter Property="Foreground" Value="Red" />
        </DataTrigger>

        <DataTrigger Binding="{Binding Level,Mode=OneWay}" Value="{x:StaticCommon:LoggingLevel.Warning}">
            <Setter Property="Foreground" Value="Orange" />
        </DataTrigger>
    </Style.Triggers>
</Style>

<ListView ItemsSource="{Binding Entries}">
     <ListView.ItemTemplate>
         <DataTemplate>
             <TextBlock Text="{Binding Text,Mode=OneWay}" Style="{DynamicResource txtID}"/>
         </DataTemplate>
     </ListView.ItemTemplate>
</ListView>
于 2013-06-09T05:36:44.020 に答える