Skip to content

Commit f56b168

Browse files
WIP on fix.
Scroll position is now synced correctly, but the visibility property still needs to be respected properly.
1 parent be4153c commit f56b168

File tree

2 files changed

+91
-5
lines changed

2 files changed

+91
-5
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using Microsoft.Xaml.Behaviors;
2+
3+
namespace MaterialDesignThemes.Wpf.Behaviors;
4+
5+
internal class TextBoxHorizontalScrollBarBehavior : Behavior<ScrollViewer>
6+
{
7+
public static readonly DependencyProperty TargetScrollBarProperty =
8+
DependencyProperty.Register(nameof(TargetScrollBar), typeof(ScrollBar), typeof(TextBoxHorizontalScrollBarBehavior), new PropertyMetadata(null, TargetScrollBarChanged));
9+
public ScrollBar TargetScrollBar
10+
{
11+
get => (ScrollBar)GetValue(TargetScrollBarProperty);
12+
set => SetValue(TargetScrollBarProperty, value);
13+
}
14+
15+
private static void TargetScrollBarChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
16+
{
17+
TextBoxHorizontalScrollBarBehavior b = (TextBoxHorizontalScrollBarBehavior)d;
18+
19+
if (e.OldValue is ScrollBar oldValue)
20+
{
21+
oldValue.Scroll -= b.TargetScrollBar_OnScroll;
22+
}
23+
if (e.NewValue is ScrollBar newValue)
24+
{
25+
newValue.Scroll += b.TargetScrollBar_OnScroll;
26+
}
27+
}
28+
29+
private void TargetScrollBar_OnScroll(object sender, ScrollEventArgs e)
30+
{
31+
if (AssociatedObject is not { } ao) return;
32+
ao.ScrollToHorizontalOffset(e.NewValue);
33+
}
34+
35+
private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
36+
=> AssociatedObject.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden;
37+
38+
private void AssociatedObject_SizeChanged(object sender, SizeChangedEventArgs e)
39+
{
40+
if (TargetScrollBar is not { } ts) return;
41+
42+
ts.ViewportSize = AssociatedObject.ViewportWidth;
43+
ts.Maximum = AssociatedObject.ScrollableWidth;
44+
}
45+
46+
private void AssociatedObject_ScrollChanged(object sender, ScrollChangedEventArgs e)
47+
{
48+
if (TargetScrollBar is not { } ts) return;
49+
50+
ts.Value = AssociatedObject.HorizontalOffset;
51+
}
52+
53+
protected override void OnAttached()
54+
{
55+
base.OnAttached();
56+
AssociatedObject.Loaded += AssociatedObject_Loaded;
57+
AssociatedObject.SizeChanged += AssociatedObject_SizeChanged;
58+
AssociatedObject.ScrollChanged += AssociatedObject_ScrollChanged;
59+
}
60+
61+
62+
protected override void OnDetaching()
63+
{
64+
if (AssociatedObject is { } ao)
65+
{
66+
ao.Loaded -= AssociatedObject_Loaded;
67+
ao.SizeChanged -= AssociatedObject_SizeChanged;
68+
ao.ScrollChanged -= AssociatedObject_ScrollChanged;
69+
}
70+
base.OnDetaching();
71+
}
72+
}

src/MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.TextBox.xaml

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
xmlns:convertersInternal="clr-namespace:MaterialDesignThemes.Wpf.Converters.Internal"
55
xmlns:internal="clr-namespace:MaterialDesignThemes.Wpf.Internal"
66
xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf"
7-
xmlns:behaviors="clr-namespace:MaterialDesignThemes.Wpf.Behaviors">
7+
xmlns:behaviors="clr-namespace:MaterialDesignThemes.Wpf.Behaviors"
8+
xmlns:b="http://schemas.microsoft.com/xaml/behaviors">
89
<ResourceDictionary.MergedDictionaries>
910
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.ValidationErrorTemplate.xaml" />
1011
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Button.xaml" />
@@ -131,7 +132,13 @@
131132
CornerRadius="{TemplateBinding wpf:TextFieldAssist.TextFieldCornerRadius}"
132133
SnapsToDevicePixels="True">
133134

134-
<Grid x:Name="ContentGrid"
135+
<Grid>
136+
<Grid.RowDefinitions>
137+
<RowDefinition Height="*" />
138+
<RowDefinition Height="Auto" />
139+
</Grid.RowDefinitions>
140+
141+
<Grid x:Name="ContentGrid"
135142
MinHeight="16"
136143
VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
137144
<Grid.ColumnDefinitions>
@@ -153,7 +160,7 @@
153160
Opacity="{TemplateBinding wpf:HintAssist.HintOpacity}"
154161
Visibility="{TemplateBinding wpf:TextFieldAssist.HasLeadingIcon, Converter={x:Static converters:BooleanToVisibilityConverter.CollapsedInstance}}" />
155162

156-
<ScrollViewer x:Name="PART_ContentHost"
163+
<ScrollViewer x:Name="PART_ContentHost"
157164
Grid.Column="2"
158165
HorizontalAlignment="Stretch"
159166
VerticalAlignment="Center"
@@ -166,9 +173,13 @@
166173
HorizontalScrollBarVisibility="Hidden"
167174
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
168175
UseLayoutRounding="{TemplateBinding UseLayoutRounding}"
169-
VerticalScrollBarVisibility="Hidden" />
176+
VerticalScrollBarVisibility="Hidden">
177+
<b:Interaction.Behaviors>
178+
<behaviors:TextBoxHorizontalScrollBarBehavior TargetScrollBar="{Binding ElementName=CustomScrollBar}"/>
179+
</b:Interaction.Behaviors>
180+
</ScrollViewer>
170181

171-
<wpf:SmartHint x:Name="Hint"
182+
<wpf:SmartHint x:Name="Hint"
172183
Grid.Column="1"
173184
Grid.ColumnSpan="3"
174185
VerticalAlignment="Center"
@@ -290,6 +301,9 @@
290301
Height="auto"
291302
Width="auto" />
292303
</Button>
304+
</Grid>
305+
<ScrollBar x:Name="CustomScrollBar" Grid.Row="1" Orientation="Horizontal" />
306+
293307
</Grid>
294308
</Border>
295309
</AdornerDecorator>

0 commit comments

Comments
 (0)