Skip to content

Commit

Permalink
Improve Example XAML
Browse files Browse the repository at this point in the history
- Renders now on initialization
- Replaces [PropertyName] with the Value
- A custom Converter can be used to provide e.g. {DynamicResoutce AnyKeyHere}
  • Loading branch information
timunie authored and punker76 committed Jun 4, 2020
1 parent 5bb6dcf commit 1214fe3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
22 changes: 19 additions & 3 deletions src/MahApps.Metro.Demo_v2/Controls/DemoView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class DemoView : HeaderedContentControl
private TextEditor PART_AvalonEdit;

/// <summary>Identifies the <see cref="ExampleXaml"/> dependency property.</summary>
public static readonly DependencyProperty ExampleXamlProperty = DependencyProperty.Register(nameof(ExampleXaml), typeof(string), typeof(DemoView), new PropertyMetadata(null));
public static readonly DependencyProperty ExampleXamlProperty = DependencyProperty.Register(nameof(ExampleXaml), typeof(string), typeof(DemoView), new PropertyMetadata(null, OnExampleXamlChanged));

/// <summary>Identifies the <see cref="HyperlinkOnlineDocs"/> dependency property.</summary>
public static readonly DependencyProperty HyperlinkOnlineDocsProperty = DependencyProperty.Register(nameof(HyperlinkOnlineDocs), typeof(string), typeof(DemoView), new PropertyMetadata(null));

Expand Down Expand Up @@ -67,7 +67,22 @@ private void SetExampleXaml()
{
if (PART_AvalonEdit != null)
{
PART_AvalonEdit.Text = ExampleXaml;
var exampleText = ExampleXaml;

foreach (var item in DemoProperties)
{
exampleText = exampleText.Replace($"[{item.PropertyName}]", item.GetExampleXamlContent());
}

PART_AvalonEdit.Text = exampleText;
}
}

private static void OnExampleXamlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is DemoView demoView)
{
demoView.SetExampleXaml();
}
}

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

PART_AvalonEdit = GetTemplateChild(nameof(PART_AvalonEdit)) as TextEditor;
SetExampleXaml();
}

}
Expand Down
16 changes: 13 additions & 3 deletions src/MahApps.Metro.Demo_v2/Controls/DemoViewProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ private static void OnValueChanged(DependencyObject d, DependencyPropertyChanged

public DemoViewProperty()
{

GetExampleXamlContent = GetExampleXamlContent_Default;
}

public DemoViewProperty(DependencyProperty dependencyProperty, DependencyObject bindingTarget, string groupName = null, DataTemplate dataTemplate = null)
public DemoViewProperty(DependencyProperty dependencyProperty, DependencyObject bindingTarget, string groupName = null, DataTemplate dataTemplate = null) : this()
{
SetCurrentValue(PropertyNameProperty, GetDefaultPropertyName(dependencyProperty));

Expand Down Expand Up @@ -68,7 +68,7 @@ public DemoViewProperty(DependencyProperty dependencyProperty, DependencyObject

/// <summary>Identifies the <see cref="DataTemplate"/> dependency property.</summary>
public static readonly DependencyProperty DataTemplateProperty = DependencyProperty.Register(nameof(DataTemplate), typeof(DataTemplate), typeof(DemoViewProperty), new PropertyMetadata(null));

/// <summary>Identifies the <see cref="ItemSource"/> dependency property.</summary>
public static readonly DependencyProperty ItemSourceProperty = DependencyProperty.Register(nameof(ItemSource), typeof(IEnumerable), typeof(DemoViewProperty), new PropertyMetadata(null));

Expand Down Expand Up @@ -197,5 +197,15 @@ public IEnumerable ItemSource
set { SetValue(ItemSourceProperty, value); }
}


#region XAML Replace Value
public Func<string> GetExampleXamlContent { get; set; }

private string GetExampleXamlContent_Default()
{
return Value?.ToString();
}
#endregion

}
}
8 changes: 4 additions & 4 deletions src/MahApps.Metro.Demo_v2/ExampleViews/BadgedExample.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
<ctrls:DemoView x:Name="demoView"
xml:space="preserve"
HyperlinkOnlineDocs="https://mahapps.com/docs/controls/badgedcontrol">
<ctrls:DemoView.ExampleXaml><![CDATA[<mah:Badged x:Name="badge" Badge="1">
<ctrls:DemoView.ExampleXaml><![CDATA[<mah:Badged Badge="[Badge]">
<Button Margin="5"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
HorizontalAlignment="[HorizontalAlignment]"
VerticalAlignment="[VerticalAlignment]"
Click="Button_Click"
Content="Click Me"
Content="[Content]"
IsEnabled="True" />
</mah:Badged> ]]>
</ctrls:DemoView.ExampleXaml>
Expand Down

0 comments on commit 1214fe3

Please sign in to comment.