Skip to content

Commit 195565d

Browse files
timuniepunker76
authored andcommitted
Improve Example XAML
- Renders now on initialization - Replaces [PropertyName] with the Value - A custom Converter can be used to provide e.g. {DynamicResoutce AnyKeyHere}
1 parent 99bbee9 commit 195565d

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

src/MahApps.Metro.Demo_v2/Controls/DemoView.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public class DemoView : HeaderedContentControl
1818
private TextEditor PART_AvalonEdit;
1919

2020
/// <summary>Identifies the <see cref="ExampleXaml"/> dependency property.</summary>
21-
public static readonly DependencyProperty ExampleXamlProperty = DependencyProperty.Register(nameof(ExampleXaml), typeof(string), typeof(DemoView), new PropertyMetadata(null));
22-
21+
public static readonly DependencyProperty ExampleXamlProperty = DependencyProperty.Register(nameof(ExampleXaml), typeof(string), typeof(DemoView), new PropertyMetadata(null, OnExampleXamlChanged));
22+
2323
/// <summary>Identifies the <see cref="HyperlinkOnlineDocs"/> dependency property.</summary>
2424
public static readonly DependencyProperty HyperlinkOnlineDocsProperty = DependencyProperty.Register(nameof(HyperlinkOnlineDocs), typeof(string), typeof(DemoView), new PropertyMetadata(null));
2525

@@ -67,7 +67,22 @@ private void SetExampleXaml()
6767
{
6868
if (PART_AvalonEdit != null)
6969
{
70-
PART_AvalonEdit.Text = ExampleXaml;
70+
var exampleText = ExampleXaml;
71+
72+
foreach (var item in DemoProperties)
73+
{
74+
exampleText = exampleText.Replace($"[{item.PropertyName}]", item.GetExampleXamlContent());
75+
}
76+
77+
PART_AvalonEdit.Text = exampleText;
78+
}
79+
}
80+
81+
private static void OnExampleXamlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
82+
{
83+
if (d is DemoView demoView)
84+
{
85+
demoView.SetExampleXaml();
7186
}
7287
}
7388

@@ -86,6 +101,7 @@ public override void OnApplyTemplate()
86101
base.OnApplyTemplate();
87102

88103
PART_AvalonEdit = GetTemplateChild(nameof(PART_AvalonEdit)) as TextEditor;
104+
SetExampleXaml();
89105
}
90106

91107
}

src/MahApps.Metro.Demo_v2/Controls/DemoViewProperty.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ private static void OnValueChanged(DependencyObject d, DependencyPropertyChanged
3636

3737
public DemoViewProperty()
3838
{
39-
39+
GetExampleXamlContent = GetExampleXamlContent_Default;
4040
}
4141

42-
public DemoViewProperty(DependencyProperty dependencyProperty, DependencyObject bindingTarget, string groupName = null, DataTemplate dataTemplate = null)
42+
public DemoViewProperty(DependencyProperty dependencyProperty, DependencyObject bindingTarget, string groupName = null, DataTemplate dataTemplate = null) : this()
4343
{
4444
SetCurrentValue(PropertyNameProperty, GetDefaultPropertyName(dependencyProperty));
4545

@@ -68,7 +68,7 @@ public DemoViewProperty(DependencyProperty dependencyProperty, DependencyObject
6868

6969
/// <summary>Identifies the <see cref="DataTemplate"/> dependency property.</summary>
7070
public static readonly DependencyProperty DataTemplateProperty = DependencyProperty.Register(nameof(DataTemplate), typeof(DataTemplate), typeof(DemoViewProperty), new PropertyMetadata(null));
71-
71+
7272
/// <summary>Identifies the <see cref="ItemSource"/> dependency property.</summary>
7373
public static readonly DependencyProperty ItemSourceProperty = DependencyProperty.Register(nameof(ItemSource), typeof(IEnumerable), typeof(DemoViewProperty), new PropertyMetadata(null));
7474

@@ -197,5 +197,15 @@ public IEnumerable ItemSource
197197
set { SetValue(ItemSourceProperty, value); }
198198
}
199199

200+
201+
#region XAML Replace Value
202+
public Func<string> GetExampleXamlContent { get; set; }
203+
204+
private string GetExampleXamlContent_Default()
205+
{
206+
return Value?.ToString();
207+
}
208+
#endregion
209+
200210
}
201211
}

src/MahApps.Metro.Demo_v2/ExampleViews/BadgedExample.xaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
<ctrls:DemoView x:Name="demoView"
1313
xml:space="preserve"
1414
HyperlinkOnlineDocs="https://mahapps.com/docs/controls/badgedcontrol">
15-
<ctrls:DemoView.ExampleXaml><![CDATA[<mah:Badged x:Name="badge" Badge="1">
15+
<ctrls:DemoView.ExampleXaml><![CDATA[<mah:Badged Badge="[Badge]">
1616
<Button Margin="5"
17-
HorizontalAlignment="Stretch"
18-
VerticalAlignment="Center"
17+
HorizontalAlignment="[HorizontalAlignment]"
18+
VerticalAlignment="[VerticalAlignment]"
1919
Click="Button_Click"
20-
Content="Click Me"
20+
Content="[Content]"
2121
IsEnabled="True" />
2222
</mah:Badged> ]]>
2323
</ctrls:DemoView.ExampleXaml>

0 commit comments

Comments
 (0)