Skip to content

Commit 5e37f1b

Browse files
Simplify the InPlaceTextEditorAdorner sample
Note: This sample doesn't work on UWP for some weird issue with Popup.PlacementTarget at runtime, even though it should be supported according to the docs.
1 parent d79c2df commit 5e37f1b

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

components/Adorners/samples/InPlaceTextEditor/InPlaceTextEditorAdornerSample.xaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
1+
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
22
<Page x:Class="AdornersExperiment.Samples.InPlaceTextEditor.InPlaceTextEditorAdornerSample"
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
@@ -70,13 +70,13 @@
7070
<StackPanel Spacing="8">
7171
<TextBlock Text="Click the editable text below to edit:" />
7272

73-
<!-- We set the DataContext here for our Adorner to retrieve the IEditableObject reference -->
7473
<!-- We use TwoWay binding to ensure the updates from the Adorner are reflected in the ViewModel -->
75-
<TextBlock DataContext="{x:Bind ViewModel}"
76-
Text="{x:Bind ViewModel.MyText, Mode=TwoWay}">
74+
<TextBlock Text="{x:Bind ViewModel.MyText, Mode=TwoWay}">
7775
<ui:AdornerLayer.Xaml>
76+
<!-- We set the DataContext here for our Adorner to retrieve the IEditableObject reference -->
7877
<!-- Style manually set here as local to example -->
79-
<local:InPlaceTextEditorAdorner Style="{StaticResource DefaultInPlaceTextEditorAdornerStyle}" />
78+
<local:InPlaceTextEditorAdorner EditableObject="{x:Bind ViewModel}"
79+
Style="{StaticResource DefaultInPlaceTextEditorAdornerStyle}" />
8080
</ui:AdornerLayer.Xaml>
8181
</TextBlock>
8282
</StackPanel>

components/Adorners/samples/InPlaceTextEditor/InPlaceTextEditorAdornerSample.xaml.cs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ public void EndEdit()
6464
/// </summary>
6565
public sealed partial class InPlaceTextEditorAdorner : Adorner<TextBlock>
6666
{
67+
/// <summary>
68+
/// Gets or sets the object being edited.
69+
/// </summary>
70+
public IEditableObject EditableObject
71+
{
72+
get { return (IEditableObject)GetValue(EditableObjectProperty); }
73+
set { SetValue(EditableObjectProperty, value); }
74+
}
75+
76+
/// <summary>
77+
/// Identifies the <see cref="EditableObject"/> dependency property.
78+
/// </summary>
79+
public static readonly DependencyProperty EditableObjectProperty =
80+
DependencyProperty.Register(nameof(EditableObject), typeof(IEditableObject), typeof(InPlaceTextEditorAdorner), new PropertyMetadata(null));
81+
6782
/// <summary>
6883
/// Gets or sets whether the popup is open.
6984
/// </summary>
@@ -108,28 +123,19 @@ protected override void OnDetaching()
108123

109124
private void AdornedElement_Tapped(object sender, TappedRoutedEventArgs e)
110125
{
111-
if (AdornedElement?.DataContext is IEditableObject editableObject)
112-
{
113-
editableObject.BeginEdit();
114-
}
126+
EditableObject?.BeginEdit();
115127
IsPopupOpen = true;
116128
}
117129

118130
public void ConfirmButton_Click(object sender, RoutedEventArgs e)
119131
{
120-
if (AdornedElement?.DataContext is IEditableObject editableObject)
121-
{
122-
editableObject.EndEdit();
123-
}
132+
EditableObject?.EndEdit();
124133
IsPopupOpen = false;
125134
}
126135

127136
public void CloseButton_Click(object sender, RoutedEventArgs e)
128137
{
129-
if (AdornedElement?.DataContext is IEditableObject editableObject)
130-
{
131-
editableObject.CancelEdit();
132-
}
138+
EditableObject?.CancelEdit();
133139
IsPopupOpen = false;
134140
}
135141
}

0 commit comments

Comments
 (0)