我有2个自定义控件:1个用于设计包装控件,第二个是自定义richtextbox控件。 richtextbox正在从一个文件加载数据,现在对于每个Run控件里面我想检查他的父(Span控件)的FontFamily字段,如果值是“Symbol”,那么请做一些,但我看到数据绑定是'被解雇了,如何纠正?
<Style TargetType="{x:Type local:HelpCtrl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:HelpCtrl}">
<Grid>
<Grid x:Name="gridRichTextBox" Background="Black" Width="350" Height="600">
<local:RichTextBoxWithTextProperty
IsReadOnly="True"
x:Name="rtbHelpText"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto"
Text="{Binding Text, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:HelpCtrl}}}">
<local:RichTextBoxWithTextProperty.Resources>
<Style TargetType="{x:Type Run}">
<Setter Property="Background" Value="Green"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Span}},Path=FontFamily}" Value="Symbol">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</local:RichTextBoxWithTextProperty.Resources>
</local:RichTextBoxWithTextProperty>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
答案 0 :(得分:0)
问题出在其他地方,因为你的代码工作正常。
<Style TargetType="{x:Type local:HelpCtrl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:HelpCtrl}">
<Grid>
<Grid x:Name="gridRichTextBox">
<RichTextBox>
<RichTextBox.Resources>
<Style TargetType="{x:Type Run}">
<Setter Property="Background" Value="Green"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Span}},Path=FontFamily}" Value="Symbol">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</RichTextBox.Resources>
<FlowDocument>
<Paragraph>
<Span FontFamily="Tahoma">Some text</Span>
<Span FontFamily="Symbol">Some symbol</Span>
</Paragraph>
</FlowDocument>
</RichTextBox>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
确保您没有在自己的类中设置CustomControl的DataContext。你必须在xaml中使用它进行设置:
<local:RichTextBoxWithTextProperty DataContext="{Binding ...}" Text="{Binding Text...}">
答案 1 :(得分:0)
@Bizz,是的,如果我删除绑定,Run的样式就像预期的那样绘制..我试图填充块的可观察集合:
static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MemoryStream stream = new MemoryStream(File.ReadAllBytes(e.NewValue.ToString()));
RichTextBox r = new RichTextBox();
r.Selection.Load(stream, DataFormats.Rtf);
HelpCtrl helpCtrl = d as HelpCtrl;
ObservableCollection<Block> l = new ObservableCollection<Block>();
while (r.Document.Blocks.Count > 0)
{
var block = r.Document.Blocks.FirstBlock;
r.Document.Blocks.Remove(block);
l.Add(block);
}
helpCtrl.Blocks = l;
}
public ObservableCollection<Block> Blocks
{
get { return (ObservableCollection<Block>)GetValue(BlocksProperty); }
set { SetValue(BlocksProperty, value); }
}
// Using a DependencyProperty as the backing store for Blocks. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BlocksProperty =
DependencyProperty.Register("Blocks", typeof(ObservableCollection<Block>), typeof(HelpCtrl), new UIPropertyMetadata(null));
<Style TargetType="{x:Type local:HelpCtrl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:HelpCtrl}">
<Grid>
<Grid x:Name="gridRichTextBox" Background="Black" Width="350" Height="600">
<RichTextBox
IsReadOnly="True"
x:Name="rtbHelpText"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto"
>
<FlowDocument>
<FlowDocument.Resources>
<Style TargetType="{x:Type Run}">
<Setter Property="Background" Value="Green"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Span}},Path=FontFamily}" Value="Symbol">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
<DataTemplate DataType="{x:Type Paragraph}">
<ItemsControl ItemsSource="{Binding Inlines}" IsHitTestVisible="False">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</DataTemplate>
</FlowDocument.Resources>
<Paragraph>
<ItemsControl ItemsSource="{Binding Blocks, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:HelpCtrl}}}"/>
</Paragraph>
</FlowDocument>
</RichTextBox>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
但现在跑步的风格并不是什么都没画......