FlowDocument を生成しようとしていますが、特定の文字列が含まれる特定のテキスト ランを強調表示したいと考えています。BindableRunクラスを使用して、Run テキスト値を確実に更新できるようにしています (ただし、それらは構築時にのみ設定するため、Run のバニラ インスタンスで問題ありません)。
基本的に、私はこれをしたい:
<FlowDocument Name="FlowDoc" FontFamily="{x:Static SystemFonts.CaptionFontFamily}"
Background="{x:Static SystemColors.ControlBrush}"
FontSize="{x:Static SystemFonts.SmallCaptionFontSize}"
TextAlignment="Left">
<FlowDocument.Resources>
<Style TargetType="{x:Type local:BindableRun}">
<!-- 1. Style all BindableRuns with a pink background by default -->
<Setter Property="Background" Value="HotPink"/>
<Style.Triggers>
<!-- 2. Style BindableRuns where Text=Hello to have a green background-->
<DataTrigger Binding="{Binding BoundText}" Value="Hello">
<Setter Property="Background" Value="GreenYellow"/>
</DataTrigger>
<!-- 3. Fire a Datatrigger with parameterised converter to change
text to Orange if the text contains 'StackOverflow'-->
<DataTrigger Binding="{Binding BoundText,
Converter={StaticResource ContainsBoolConverter},
ConverterParameter=StackOverflow}" Value="False">
<Setter Property="Foreground" Value="Orange"/>
</DataTrigger>
</Style.Triggers>
</Style>
</FlowDocument.Resources>
</FlowDocument>
ここで、上記の Xaml を使用しようとすると、次のようになります。
• BindableRun オブジェクト (つまり、#1) の背景を変更するスタイルは正常に機能します。したがって、FlowDocument の内容は期待どおりです。
• BindableRun オブジェクトの BountText プロパティに対する DataTriggers が機能しません。
• コンバーターは呼び出されていない (ブレークポイントを設定した) ため、バインディング/データトリガーが起動していないように見えます。
誰かが前にこのようなことをしましたか? もしそうなら、あなたはそれを機能させることができましたか?SOなどのFlowDocumentの例はそれほど多くないため、RunのTextプロパティでDataTriggersを使用しようとした(成功したかどうかにかかわらず)他の人を追跡できませんでした。
参考までに、私が使用しているContainsコンバーターを次に示します(ただし、まったく起動しないため、正しいかどうかは関係ありません;))。
public class ContainsTextConverter : IValueConverter
{
public object Convert( object value, Type t, object parameter, CultureInfo culture )
{
String input = value.ToString();
string param = parameter.ToString();
return input.IndexOf( param, StringComparison.OrdinalIgnoreCase ) != -1;
}
public object ConvertBack( object value, Type t, object parameter, CultureInfo culture )
{
// Don't care about this
throw new NotSupportedException();
}
};