Skip to content

Commit

Permalink
Use ContentPresenter
Browse files Browse the repository at this point in the history
  • Loading branch information
ghost1372 committed May 19, 2024
1 parent 58e3534 commit ed78d49
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
17 changes: 1 addition & 16 deletions WinUIGallery/Controls/ControlExample.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -172,22 +172,7 @@

<!-- using animations:Implicit... with Grid.Row causes Grid.Row to have no effect, Therefore, the Grid.Row is applied in the grid -->
<Grid x:DefaultBindMode="OneWay" Grid.Row="1">
<controls:SampleCodePresenter
x:Name="XamlPresenter"
Visibility="Collapsed"
Code="{x:Bind Xaml}"
CodeSourceFile="{x:Bind XamlSource}"
Substitutions="{x:Bind Substitutions}"
animations:Implicit.HideAnimations="{StaticResource HideTransitions}"
animations:Implicit.ShowAnimations="{StaticResource ShowTransitions}"/>
<controls:SampleCodePresenter
x:Name="CSharpPresenter"
Visibility="Collapsed"
Code="{x:Bind CSharp}"
CodeSourceFile="{x:Bind CSharpSource}"
Substitutions="{x:Bind Substitutions}"
animations:Implicit.HideAnimations="{StaticResource HideTransitions}"
animations:Implicit.ShowAnimations="{StaticResource ShowTransitions}"/>
<ContentPresenter x:Name="SamplePresenterHost"/>
</Grid>
</Grid>
</muxc:Expander>
Expand Down
35 changes: 28 additions & 7 deletions WinUIGallery/Controls/ControlExample.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
using Microsoft.UI.Xaml.Markup;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
using WinUIGallery.Controls;
using CommunityToolkit.WinUI.Animations;

namespace WinUIGallery
{
Expand Down Expand Up @@ -443,7 +445,7 @@ private void SelectorBarItem_Loaded(object sender, RoutedEventArgs e)
firstVisibileItem.IsSelected = true;
}

HandlePresenterVisibility();
SwitchPresenter();
}

private static void OnXamlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
Expand All @@ -465,25 +467,44 @@ private static void OnCSharpChanged(DependencyObject d, DependencyPropertyChange

private void SelectorBarControl_SelectionChanged(SelectorBar sender, SelectorBarSelectionChangedEventArgs args)
{
HandlePresenterVisibility();
SwitchPresenter();
}

private void HandlePresenterVisibility()
private void SwitchPresenter()
{
var selectedItem = SelectorBarControl.SelectedItem;
if (selectedItem != null)
{
if (selectedItem.Tag.ToString().Equals("Xaml", StringComparison.OrdinalIgnoreCase))
{
XamlPresenter.Visibility = Visibility.Visible;
CSharpPresenter.Visibility = Visibility.Collapsed;
SamplePresenterHost.Content = CreateSamplePresenter(SampleCodePresenterType.XAML);
}
else if (selectedItem.Tag.ToString().Equals("CSharp", StringComparison.OrdinalIgnoreCase))
{
CSharpPresenter.Visibility = Visibility.Visible;
XamlPresenter.Visibility = Visibility.Collapsed;
SamplePresenterHost.Content = CreateSamplePresenter(SampleCodePresenterType.CSharp);
}
}
}

private SampleCodePresenter CreateSamplePresenter(SampleCodePresenterType presenterType)
{
var presenter = new SampleCodePresenter();
switch (presenterType)
{
case SampleCodePresenterType.XAML:
presenter.Code = Xaml;
presenter.CodeSourceFile = XamlSource;
break;
case SampleCodePresenterType.CSharp:
presenter.Code = CSharp;
presenter.CodeSourceFile = CSharpSource;
break;
}

presenter.Substitutions = Substitutions;
Implicit.SetHideAnimations(presenter, Resources["HideTransitions"] as ImplicitAnimationSet);
Implicit.SetShowAnimations(presenter, Resources["ShowTransitions"] as ImplicitAnimationSet);
return presenter;
}
}
}

0 comments on commit ed78d49

Please sign in to comment.