Skip to content

Commit 1fae450

Browse files
Ensure behavior does not break Visual Studio XAML Designer
1 parent 960301a commit 1fae450

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/MaterialDesignThemes.Wpf/Behaviors/Internal/TabControlHeaderScrollBehavior.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.ComponentModel;
12
using System.Windows.Media.Animation;
23
using Microsoft.Xaml.Behaviors;
34

@@ -12,6 +13,9 @@ public class TabControlHeaderScrollBehavior : Behavior<ScrollViewer>
1213
public static void SetCustomHorizontalOffset(DependencyObject obj, double value) => obj.SetValue(CustomHorizontalOffsetProperty, value);
1314
private static void CustomHorizontalOffsetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
1415
{
16+
if (DesignerProperties.GetIsInDesignMode(d))
17+
return;
18+
1519
var scrollViewer = (ScrollViewer)d;
1620
scrollViewer.ScrollToHorizontalOffset((double)e.NewValue);
1721
}
@@ -35,6 +39,9 @@ public TabControl TabControl
3539

3640
private static void OnTabControlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
3741
{
42+
if (DesignerProperties.GetIsInDesignMode(d))
43+
return;
44+
3845
var behavior = (TabControlHeaderScrollBehavior)d;
3946
if (e.OldValue is TabControl oldTabControl)
4047
{
@@ -64,6 +71,9 @@ public double AdditionalHeaderPanelContentWidth
6471

6572
private static void AdditionalHeaderPanelContentWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
6673
{
74+
if (DesignerProperties.GetIsInDesignMode(d))
75+
return;
76+
6777
var behavior = (TabControlHeaderScrollBehavior)d;
6878
behavior.AddPaddingToScrollableContentIfWiderThanViewPort();
6979
}
@@ -80,6 +90,9 @@ public double NavigationPanelLeftWidth
8090

8191
private static void NavigationPanelLeftWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
8292
{
93+
if (DesignerProperties.GetIsInDesignMode(d))
94+
return;
95+
8396
var behavior = (TabControlHeaderScrollBehavior)d;
8497
behavior.AddPaddingToScrollableContentIfWiderThanViewPort();
8598
}
@@ -96,6 +109,9 @@ public FrameworkElement ScrollableContent
96109

97110
private static void OnScrollableContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
98111
{
112+
if (DesignerProperties.GetIsInDesignMode(d))
113+
return;
114+
99115
var behavior = (TabControlHeaderScrollBehavior)d;
100116
behavior.AddPaddingToScrollableContentIfWiderThanViewPort();
101117
}
@@ -108,6 +124,13 @@ private static void OnScrollableContentChanged(DependencyObject d, DependencyPro
108124

109125
public TabControlHeaderScrollBehavior()
110126
{
127+
if (DesignerProperties.GetIsInDesignMode(this))
128+
{
129+
NextTabCommand = null!;
130+
PreviousTabCommand = null!;
131+
return;
132+
}
133+
111134
NextTabCommand = new SimpleICommandImplementation(_ =>
112135
{
113136
if (TabControl is { } tabControl)
@@ -280,6 +303,9 @@ private void OnTabControlPreviewKeyDown(object sender, KeyEventArgs e)
280303
protected override void OnAttached()
281304
{
282305
base.OnAttached();
306+
if (DesignerProperties.GetIsInDesignMode(this))
307+
return;
308+
283309
AssociatedObject.ScrollChanged += AssociatedObject_ScrollChanged;
284310
AssociatedObject.SizeChanged += AssociatedObject_SizeChanged;
285311
Dispatcher.BeginInvoke(() => AddPaddingToScrollableContentIfWiderThanViewPort());
@@ -288,6 +314,9 @@ protected override void OnAttached()
288314
protected override void OnDetaching()
289315
{
290316
base.OnDetaching();
317+
if (DesignerProperties.GetIsInDesignMode(this))
318+
return;
319+
291320
if (AssociatedObject is { } ao)
292321
{
293322
ao.ScrollChanged -= AssociatedObject_ScrollChanged;

0 commit comments

Comments
 (0)