gpt4 book ai didi

c# - (附加行为的)依赖属性未被附加属性(另一个附加行为的)更新

转载 作者:行者123 更新时间:2023-11-30 18:17:40 25 4
gpt4 key购买 nike

WPF。

在 XAML 中,我有一个 Canvas,它将容纳 TextBlock,其位置由从 RichTextBox 控件收集的信息决定。 Canvas 和 RichTextBox 都有添加“Outline”属性的附加行为。

我的目标是让 RichTextBox 从其 RichTextBoxSelectionChanged 事件中设置 outline 属性。当文本被输入到 RichTextBox 中时,outline 属性将不断变化。

我需要“OutlineCanvas”来获取 Outline 属性中的更改,并通过将 TextBlocks 放置在关联的 Canvas 中来对其进行操作。

由于我无法理解的原因,下面的代码确实在 RichTextBox 中设置了“大纲”,但它没有被 OutlineCanvas 拾取。

似乎 OutlineCanvas 的行为似乎只启动了一次,但它没有获取在 RichTextBox 行为中设置的大纲。

我哪里错了?应该怎么做?

TIA

XAML
<Canvas Grid.Column="0" Name="OutlineCanvas" Background="Azure" >
<i:Interaction.Behaviors>
<b:OutlineBehavior Outline="{Binding ElementName=RichTextControl, Path=(b:RichTextBehavior.Outline)}"/>
</i:Interaction.Behaviors>
</Canvas>

<RichTextBox x:Name="RichTextControl" Grid.Column="1"
a:SmartAdorner.Visible="{Binding TranscriptionLayer.IsAdornerVisible}"
Panel.ZIndex="{Binding TranscriptionLayer.ZIndex}" Cursor="IBeam"
Height="{Binding VirtualPage.Height}"
Visibility="{Binding Path=TranscriptionLayer.Visibility}"
SpellCheck.IsEnabled="True"
VerticalScrollBarVisibility="Auto"
AcceptsReturn="True" AcceptsTab="True"
>

<i:Interaction.Behaviors>
<b:RichTextBehavior
SelectedText="{Binding TranscriptionLayer.SelectedText}"
Image="{Binding TranscriptionLayer.Image}"
MoveImage="{Binding TranscriptionLayer.MoveImage}"
DeleteImage="{Binding TranscriptionLayer.DeleteImage}"
/>
</i:Interaction.Behaviors>



C#
public class OutlineBehavior : Behavior<Canvas>
{
// The XAML attaches the behavior when it is instantiated.
protected override void OnAttached()
{
base.OnAttached();
}

protected override void OnDetaching()
{
base.OnDetaching();
}



public static ProgressNoteOutline GetOutline(DependencyObject obj)
{
return (ProgressNoteOutline)obj.GetValue(OutlineProperty);
}

public static void SetOutline(DependencyObject obj, ProgressNoteOutline value)
{
obj.SetValue(OutlineProperty, value);
}

// Using a DependencyProperty as the backing store for Outline. This enables animation, styling, binding, etc...
public static readonly DependencyProperty OutlineProperty =
DependencyProperty.Register("Outline", typeof(ProgressNoteOutline), typeof(OutlineBehavior),
new FrameworkPropertyMetadata(null, OnOutlineChanged));

private static void OnOutlineChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
OutlineBehavior behavior = d as OutlineBehavior;
if (behavior == null)
return;

Canvas canv = behavior.AssociatedObject as Canvas;
if (canv == null)
return;

ProgressNoteOutline _outline = (ProgressNoteOutline)e.NewValue;
if (_outline == null)
return;

OutlineItem[] _items = _outline.items;
foreach (OutlineItem t in _items)
{
TextBlock tb = new TextBlock();
tb.Background = Brushes.AntiqueWhite;
tb.TextAlignment = TextAlignment.Left;
tb.Inlines.Add(new Italic(new Bold(new Run(t.text))));

Canvas.SetLeft(tb, 0);
Canvas.SetTop(tb, t.Y);

canv.Children.Add(tb);
}

}

The RichTextBox property (in its behavior):
public ProgressNoteOutline Outline
{
get { return (ProgressNoteOutline)GetValue(OutlineProperty); }
set { SetValue(OutlineProperty, value); }
}

// Using a DependencyProperty as the backing store for Outline. This enables animation, styling, binding, etc...
public static readonly DependencyProperty OutlineProperty =
DependencyProperty.RegisterAttached("Outline", typeof(ProgressNoteOutline), typeof(RichTextBehavior),
new FrameworkPropertyMetadata(new ProgressNoteOutline());

最后,在 RichTextBox 行为中,Outline 被设置为:

 var _items = new OutlineItem[100];
for (int i = 0; i < 100; i++)
{
_items[i] = new OutlineItem { Y = i * 30, text = string.Format("Title {0}", i) };
}
Outline = new ProgressNoteOutline { items = _items };

最佳答案

我似乎不明白这个绑定(bind)应该如何工作——从一种行为到另一种行为。因此,有效的方法是将 Canvas 子类化为:

    public class OutlineCanvas : Canvas
{

public ProgressNoteOutline Outline
{
get { return (ProgressNoteOutline)GetValue(OutlineProperty); }
set { SetValue(OutlineProperty, value); }
}

// Using a DependencyProperty as the backing store for Outline. This enables animation, styling, binding, etc...
public static readonly DependencyProperty OutlineProperty =
DependencyProperty.Register("Outline",
typeof(ProgressNoteOutline), typeof(OutlineCanvas),
new FrameworkPropertyMetadata(null, OnOutlineChanged));

private static void OnOutlineChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
OutlineCanvas canv = d as OutlineCanvas;
if (canv == null)
return;

ProgressNoteOutline _outline = (ProgressNoteOutline)e.NewValue;
if (_outline == null)
return;

OutlineItem[] _items = _outline.items;
if (_items == null)
return;

foreach (OutlineItem t in _items)
{
TextBlock tb = new TextBlock();
tb.Background = Brushes.AntiqueWhite;
tb.TextAlignment = TextAlignment.Left;
tb.Inlines.Add(new Italic(new Bold(new Run(t.text))));

SetLeft(tb, 0);
SetTop(tb, t.Y);

canv.Children.Add(tb);
}
}
}

然后将 XAML 更改为:

 <b:OutlineCanvas Grid.Column="0" x:Name="OutlineCanvas" Background="Azure" />

<b:RichTextBehavior
Outline="{Binding ElementName=OutlineCanvas, Path=Outline}" />

希望对大家有所帮助:)

关于c# - (附加行为的)依赖属性未被附加属性(另一个附加行为的)更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43038629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com