我有 richTextBox 并希望在 xmlns:i=http://schemas.microsoft.com/expression/2010/interactivity 的帮助下将命令绑定到 ScrollBar.Scroll 事件。 我尝试过,但不起作用:
<RichTextBox >
<RichTextBox.Document>
<FlowDocument>
<Paragraph>
<ScrollBar>
<behaviors:Interaction.Triggers>
<behaviors:EventTrigger EventName="Scroll">
<behaviors:InvokeCommandAction Command="{Binding UpdateText, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=views:MainWindow}}"/>
</behaviors:EventTrigger>
</behaviors:Interaction.Triggers>
</ScrollBar>
<Run Text="{Binding Text, Mode=TwoWay, UpdateSourceTrigger=LostFocus}">
</Run>
</Paragraph>
</FlowDocument>
</RichTextBox.Document>
</RichTextBox>
假设
UpdateText
是命令,并且它是 MainWindowViewModel
[DataContext
的 MainWindow
] 的属性,则绑定应该是
<behaviors:InvokeCommandAction Command="{Binding Path=DataContext.UpdateText, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/>
我还建议将
UpdateText
重命名为 UpdateTextCommand
只是为了明确它是什么。