我的目标是为FlowDocumentScrollViewer创建可重用的附加行为,以便每当FlowDocument更新(附加)时,查看器都会自动滚动到末尾。
到目前为止的问题:
我意识到那些可能是3个独立的问题(又称问题)。但是它们彼此依赖,并且我尝试了这种行为的整体设计。我问这是一个单一的问题,以防我以错误的方式解决这个问题。如果我是,那么正确的方法是什么?
/// Attached Dependency Properties not shown here:
/// bool Enabled
/// DependencyProperty DocumentProperty
/// TextRange MonitoredRange
/// ScrollViewer ScrollViewer
public static void OnEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d == null || System.ComponentModel.DesignerProperties.GetIsInDesignMode(d))
return;
DependencyProperty documentProperty = null;
ScrollViewer scrollViewer = null;
if (e.NewValue is bool && (bool)e.NewValue)
{
// Using reflection so that this will work with similar types.
FieldInfo documentFieldInfo = d.GetType().GetFields().FirstOrDefault((m) => m.Name == "DocumentProperty");
documentProperty = documentFieldInfo.GetValue(d) as DependencyProperty;
// doesn't work. the visual tree hasn't been built yet
scrollViewer = FindScrollViewer(d);
}
if (documentProperty != d.GetValue(DocumentPropertyProperty) as DependencyProperty)
d.SetValue(DocumentPropertyProperty, documentProperty);
if (scrollViewer != d.GetValue(ScrollViewerProperty) as ScrollViewer)
d.SetValue(ScrollViewerProperty, scrollViewer);
}
private static ScrollViewer FindScrollViewer(DependencyObject obj)
{
do
{
if (VisualTreeHelper.GetChildrenCount(obj) > 0)
obj = VisualTreeHelper.GetChild(obj as Visual, 0);
else
return null;
}
while (!(obj is ScrollViewer));
return obj as ScrollViewer;
}
public static void OnDocumentPropertyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.OldValue != null)
{
DependencyProperty dp = e.OldValue as DependencyProperty;
// -= OnFlowDocumentChanged
}
if (e.NewValue != null)
{
DependencyProperty dp = e.NewValue as DependencyProperty;
// += OnFlowDocumentChanged
// dp.AddOwner(typeof(AutoScrollBehavior), new PropertyMetadata(OnFlowDocumentChanged));
// System.ArgumentException was unhandled by user code Message='AutoScrollBehavior'
// type must derive from DependencyObject.
}
}
public static void OnFlowDocumentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TextRange range = null;
if (e.NewValue != null)
{
FlowDocument doc = e.NewValue as FlowDocument;
if (doc != null)
range = new TextRange(doc.ContentStart, doc.ContentEnd);
}
if (range != d.GetValue(MonitoredRangeProperty) as TextRange)
d.SetValue(MonitoredRangeProperty, range);
}
public static void OnMonitoredRangeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.OldValue != null)
{
TextRange range = e.OldValue as TextRange;
if (range != null)
range.Changed -= new EventHandler(range_Changed);
}
if (e.NewValue != null)
{
TextRange range = e.NewValue as TextRange;
if (range != null)
range.Changed -= new EventHandler(range_Changed);
}
}
static void range_Changed(object sender, EventArgs e)
{
// need ScrollViewer!!
}
答案 0 :(得分:4)
之前调用OnEnabledChanged 视觉树完成,因此 找不到ScrollViewer
在构建可视化树之后,使用Dispatcher.BeginInvoke将其余工作排入异步。您还需要调用ApplyTemplate以确保模板已实例化:
d.Dispatcher.BeginInvoke(new Action(() =>
{
((FrameworkElement)d).ApplyTemplate();
d.SetValue(ScrollViewerProperty, FindScrollViewer(d));
}));
请注意,您无需检查新值是否与旧值不同。在设置依赖项属性时,框架会为您处理。
您还可以使用FrameworkTemplate.FindName从FlowDocumentScrollViewer获取ScrollViewer。 FlowDocumentScrollViewer有一个ScrollViewer类型的命名模板部分,名为PART_ContentHost,它实际上是托管内容的地方。如果查看器被重新模板化并且作为子项具有多个ScrollViewer,则这可以更准确。
var control = d as Control;
if (control != null)
{
control.Dispatcher.BeginInvoke(new Action(() =>
{
control.ApplyTemplate();
control.SetValue(ScrollViewerProperty,
control.Template.FindName("PART_ContentHost", control)
as ScrollViewer);
}));
}
我不知道如何依附于 包含的DependencyProperty FlowDocument的。我的计划是使用它 更改了事件以初始化 ManagedRange属性。 (手动 如果是第一次触发 需要的。)
没有办法在框架中构建从任意依赖项属性获取属性更改通知。但是,您可以创建自己的DependencyProperty并将其绑定到您要观看的那个。有关详细信息,请参阅Change Notification for Dependency Properties。
创建依赖项属性:
private static readonly DependencyProperty InternalDocumentProperty =
DependencyProperty.RegisterAttached(
"InternalDocument",
typeof(FlowDocument),
typeof(YourType),
new PropertyMetadata(OnFlowDocumentChanged));
并简单地用OnEnabledChanged替换你的反射代码:
BindingOperations.SetBinding(d, InternalDocumentProperty,
new Binding("Document") { Source = d });
当FlowDocumentScrollViewer的Document属性发生更改时,绑定将更新InternalDocument,并将调用OnFlowDocumentChanged。
我不知道怎么去 ScrollViewer属性来自于 range_Changed方法,因为它没有 拥有DependencyObject。
sender属性将是TextRange,因此您可以使用((TextRange)sender).Start.Parent
来获取DependencyObject,然后沿着可视树向上走。
更简单的方法是使用lambda表达式通过执行以下操作来捕获OnMonitoredRangeChanged中的d
变量:
range.Changed += (sender, args) => range_Changed(d);
然后创建一个带有DependencyObject的range_Changed重载。但是,这会让你在完成后删除处理程序变得有点困难。
此外,虽然Detect FlowDocument Change and Scroll的答案表明TextRange.Changed可以正常工作,但我测试它时并没有看到它真的发生了。如果它不适合你并且你愿意使用反射,那么有一个似乎触发的TextContainer.Changed事件:
var container = doc.GetType().GetProperty("TextContainer",
BindingFlags.Instance | BindingFlags.NonPublic).GetValue(doc, null);
var changedEvent = container.GetType().GetEvent("Changed",
BindingFlags.Instance | BindingFlags.NonPublic);
EventHandler handler = range_Changed;
var typedHandler = Delegate.CreateDelegate(changedEvent.EventHandlerType,
handler.Target, handler.Method);
changedEvent.GetAddMethod(true).Invoke(container, new object[] { typedHandler });
sender
参数将是TextContainer,您可以再次使用反射来返回FlowDocument:
var document = sender.GetType().GetProperty("Parent",
BindingFlags.Instance | BindingFlags.NonPublic)
.GetValue(sender, null) as FlowDocument;
var viewer = document.Parent;
答案 1 :(得分:0)
这至少是一个好的开始(也许?)。