代替更好的解決方案,這個工作對我來說:
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