Skip to content

Commit 694056b

Browse files
committed
Bind the IsActive property on the SideSheet to the Visibility property
1 parent 58e4507 commit 694056b

File tree

3 files changed

+33
-28
lines changed

3 files changed

+33
-28
lines changed

WPFCustomControls/SideSheet.cs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,11 @@ public bool IsActive
2626
}
2727

2828
public static readonly DependencyProperty IsActiveProperty =
29-
DependencyProperty.Register("IsActive", typeof(bool), typeof(SideSheet), new PropertyMetadata(false, IsActiveChanged));
29+
DependencyProperty.Register("IsActive", typeof(bool), typeof(SideSheet), new PropertyMetadata(false));
3030

3131
static SideSheet()
3232
{
3333
DefaultStyleKeyProperty.OverrideMetadata(typeof(SideSheet), new FrameworkPropertyMetadata(typeof(SideSheet)));
3434
}
35-
36-
public SideSheet()
37-
{
38-
Loaded += SideSheet_Loaded;
39-
}
40-
41-
private void SideSheet_Loaded(object sender, RoutedEventArgs e)
42-
{
43-
RenderTransform = new TranslateTransform(ActualWidth, 0);
44-
}
45-
46-
private static void IsActiveChanged(DependencyObject o, DependencyPropertyChangedEventArgs args)
47-
{
48-
var sideSheet = (SideSheet)o;
49-
var isActive = (bool)args.NewValue;
50-
51-
sideSheet.RenderTransform = new TranslateTransform(isActive ? 0 : sideSheet.ActualWidth, 0);
52-
53-
/*sideSheet.SetValue(WidthProperty, DependencyProperty.UnsetValue);
54-
55-
var targetValue = isActive ? sideSheet.DesiredSize.Width : 0;
56-
57-
var animation = new DoubleAnimation() { To = targetValue, Duration = new Duration(TimeSpan.FromSeconds(2)) };
58-
sideSheet.BeginAnimation(WidthProperty, animation);*/
59-
}
6035
}
6136
}

WPFCustomControls/Themes/Generic.xaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
<ResourceDictionary
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4-
xmlns:local="clr-namespace:WPFCustomControls">
4+
xmlns:local="clr-namespace:WPFCustomControls"
5+
xmlns:converters="clr-namespace:WPFCustomControls.TypeConverters">
56
<Style TargetType="{x:Type local:SideSheet}">
67
<Setter Property="Template">
78
<Setter.Value>
89
<ControlTemplate TargetType="{x:Type local:SideSheet}">
10+
<ControlTemplate.Resources>
11+
<converters:BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter"/>
12+
</ControlTemplate.Resources>
913
<Border Background="{TemplateBinding Background}"
1014
BorderBrush="{TemplateBinding BorderBrush}"
11-
BorderThickness="{TemplateBinding BorderThickness}">
15+
BorderThickness="{TemplateBinding BorderThickness}"
16+
Visibility="{TemplateBinding IsActive, Converter={StaticResource booleanToVisibilityConverter}}">
1217
<ScrollViewer HorizontalScrollBarVisibility="Disabled"
1318
VerticalScrollBarVisibility="Auto">
1419
<ContentPresenter></ContentPresenter>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
using System.Windows.Data;
9+
10+
namespace WPFCustomControls.TypeConverters
11+
{
12+
public class BooleanToVisibilityConverter : IValueConverter
13+
{
14+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
15+
{
16+
var val = (bool)value;
17+
return val ? Visibility.Visible : Visibility.Collapsed;
18+
}
19+
20+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
21+
{
22+
throw new NotImplementedException();
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)