代替更好的解决方案,这个工作对我来说:
XAML
xmlns:h="clr-namespace:Notepad.Helpers"
<InkCanvas ... h:InkCanvasExtension.IsSelectionEnabled="True"
h:InkCanvasExtension.TheSelectedStrokes="{Binding SelectedStrokes, Mode=TwoWay}"
附加属性:
namespace Notepad.Helpers
{
public static class InkCanvasExtension
{
/*The provider class for an attached property (even if it is not registered as a dependency property) must provide static get and set accessors
* that follow the naming convention Set[AttachedPropertyName] and Get[AttachedPropertyName]. These accessors are required so that the acting XAML
* reader can recognize the property as an attribute in XAML and resolve the appropriate types.*/
#region [IsSelectionEnabled]
public static bool GetIsSelectionEnabled(DependencyObject obj)
{
return (bool)obj.GetValue(IsSelectionEnabled);
}
public static void SetIsSelectionEnabled(DependencyObject obj, bool value)
{
obj.SetValue(IsSelectionEnabled, value);
}
// In XAML, IsSelectionEnabled = "true" will call OnIsSelectionEnabled().
public static readonly DependencyProperty IsSelectionEnabled =
DependencyProperty.RegisterAttached("IsSelectionEnabled",
typeof(bool), typeof(InkCanvasExtension),
new UIPropertyMetadata(false, OnIsSelectionEnabled));
private static void OnIsSelectionEnabled(object sender, DependencyPropertyChangedEventArgs e)
{
InkCanvas ic = sender as InkCanvas;
if (ic != null)
{
// get the value of IsSelectionEnabled (which is either true or false).
bool isEnabled = (bool)e.NewValue;
if (isEnabled)
{
ic.SelectionChanged += OnSelectionChanged;
}
else
{
ic.SelectionChanged -= OnSelectionChanged;
}
}
}
private static void OnSelectionChanged(object sender, EventArgs e)
{
// Assigning TheSelectedStrokes directly like:
// TheSelectedStrokes = selectedStrokes
// will not work and will break the binding. Must use:
// SetTheSelectedStrokes(ic, selectedStrokes);
InkCanvas ic = sender as InkCanvas;
StrokeCollection selectedStrokes = ic.GetSelectedStrokes();
SetTheSelectedStrokes(ic, selectedStrokes);
}
#endregion
#region [TheSelectedStrokes]
public static StrokeCollection GetTheSelectedStrokes(DependencyObject obj)
{
return (StrokeCollection)obj.GetValue(TheSelectedStrokes);
}
public static void SetTheSelectedStrokes(DependencyObject obj, StrokeCollection value)
{
obj.SetValue(TheSelectedStrokes, value);
}
// by default binding works one way, i.e. loading changes from the view model, but not updating it back.
// so must add FrameworkPropertyMetadataOptions.BindsTwoWayByDefault to send update to the viewmodel.
public static readonly DependencyProperty TheSelectedStrokes =
DependencyProperty.RegisterAttached("TheSelectedStrokes",
typeof(StrokeCollection), typeof(InkCanvasExtension),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
#endregion
}
}
希望它可以帮助别人。
我在WPF和InkCanvas工作。我需要去办公室,但是我认为我不能访问InkPresenter或中风。已选中? –
是的。您可以使用行为以及附加属性。使用行为和附加属性的唯一区别是你使用它们的原因。例如,Grid.Row是附加在一个控件上的东西,如果控件包含在一个网格中,它就变为活动的,类似于ScrollViewer.HorizontalScrollbarVisibility等等。 如果我们想要在某些现有控件中添加某些功能,我们通常会使用行为。因此,我的建议是采取行为。但附加的财产也会做好工作..干杯:) :) 很高兴你有一个解决方案:) :) – undefined