Skip to content

Commit 43c87a5

Browse files
committed
Merge branch 'ComboBoxBackground' of https://github.com/l1pton17/MaterialDesignInXamlToolkit into l1pton17-ComboBoxBackground
2 parents d767fb0 + bb65708 commit 43c87a5

File tree

5 files changed

+103
-61
lines changed

5 files changed

+103
-61
lines changed

MaterialDesignThemes.Wpf/ComboBoxPopup.cs

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
24
using System.Linq;
35
using System.Windows;
46
using System.Windows.Controls;
57
using System.Windows.Controls.Primitives;
8+
using System.Windows.Data;
69
using System.Windows.Media;
710

811
namespace MaterialDesignThemes.Wpf
@@ -69,6 +72,23 @@ public double DownVerticalOffset
6972
set { SetValue(DownVerticalOffsetProperty, value); }
7073
}
7174

75+
#region Background property
76+
77+
public static readonly DependencyProperty BackgroundProperty
78+
= DependencyProperty.Register(nameof(Background),
79+
typeof(Brush),
80+
typeof(ComboBoxPopup),
81+
new FrameworkPropertyMetadata(Brushes.Transparent, FrameworkPropertyMetadataOptions.AffectsRender));
82+
83+
84+
public Brush Background
85+
{
86+
get { return (Brush) GetValue(BackgroundProperty); }
87+
set { SetValue(BackgroundProperty, value); }
88+
}
89+
90+
#endregion
91+
7292
#region DefaultVerticalOffset
7393

7494
public static readonly DependencyProperty DefaultVerticalOffsetProperty
@@ -117,12 +137,36 @@ private void SetChildTemplateIfNeed(ControlTemplate template)
117137
}
118138
}
119139

120-
private CustomPopupPlacement[] ComboBoxCustomPopupPlacementCallback(Size popupSize, Size targetSize,
121-
Point offset)
140+
private void SetupBackground(IEnumerable<DependencyObject> visualAncestry)
122141
{
123-
var locationFromScreen = this.PlacementTarget.PointToScreen(new Point(0, 0));
142+
var background = visualAncestry
143+
.Select(v => (v as Control)?.Background ?? (v as Border)?.Background)
144+
.FirstOrDefault(v => v != null && v != Brushes.Transparent && v is SolidColorBrush);
145+
146+
if (background != null)
147+
{
148+
Background = background;
149+
}
150+
}
124151

125-
var mainVisual = PlacementTarget.GetVisualAncestry().OfType<Visual>().LastOrDefault();
152+
private void SetupVisiblePlacementWidth(IEnumerable<DependencyObject> visualAncestry)
153+
{
154+
var parent = visualAncestry.OfType<Panel>().ElementAt(1);
155+
VisiblePlacementWidth = TreeHelper.GetVisibleWidth((FrameworkElement)PlacementTarget, parent);
156+
}
157+
158+
private CustomPopupPlacement[] ComboBoxCustomPopupPlacementCallback(
159+
Size popupSize, Size targetSize, Point offset)
160+
{
161+
var visualAncestry = PlacementTarget.GetVisualAncestry().ToList();
162+
163+
SetupBackground(visualAncestry);
164+
165+
SetupVisiblePlacementWidth(visualAncestry);
166+
167+
var locationFromScreen = PlacementTarget.PointToScreen(new Point(0, 0));
168+
169+
var mainVisual = visualAncestry.OfType<Visual>().LastOrDefault();
126170
if (mainVisual == null) return new CustomPopupPlacement[0];
127171

128172
var screenWidth = (int) DpiHelper.TransformToDeviceX(mainVisual, SystemParameters.PrimaryScreenWidth);
@@ -142,28 +186,25 @@ private CustomPopupPlacement[] ComboBoxCustomPopupPlacementCallback(Size popupSi
142186
offsetX = DpiHelper.TransformToDeviceX(mainVisual,
143187
offset.X - targetSize.Width - rtlHorizontalOffset);
144188

145-
var defaultVerticalOffsetIndepent = DpiHelper.TransformToDeviceY(mainVisual, DefaultVerticalOffset);
146-
var upVerticalOffsetIndepent = DpiHelper.TransformToDeviceY(mainVisual, UpVerticalOffset);
147-
var downVerticalOffsetIndepent = DpiHelper.TransformToDeviceY(mainVisual, DownVerticalOffset);
148-
var parent = this.PlacementTarget.GetVisualAncestry().OfType<Panel>().ElementAt(1);
149-
150-
VisiblePlacementWidth = TreeHelper.GetVisibleWidth((FrameworkElement)PlacementTarget, parent);
151189

152190
if (locationX + popupSize.Width - realOffsetX > screenWidth
153-
|| locationX + realOffsetX < 0)
191+
|| locationX - realOffsetX < 0)
154192
{
155193
SetChildTemplateIfNeed(DefaultContentTemplate);
156194

195+
var defaultVerticalOffsetIndepent = DpiHelper.TransformToDeviceY(mainVisual, DefaultVerticalOffset);
157196
var newY = locationY + popupSize.Height > screenHeight
158197
? -(defaultVerticalOffsetIndepent + popupSize.Height)
159198
: defaultVerticalOffsetIndepent + targetSize.Height;
160199

161200
return new[] { new CustomPopupPlacement(new Point(offsetX, newY), PopupPrimaryAxis.Horizontal) };
162201
}
202+
163203
if (locationY + popupSize.Height > screenHeight)
164204
{
165205
SetChildTemplateIfNeed(UpContentTemplate);
166206

207+
var upVerticalOffsetIndepent = DpiHelper.TransformToDeviceY(mainVisual, UpVerticalOffset);
167208
var newY = upVerticalOffsetIndepent - popupSize.Height + targetSize.Height;
168209

169210
return new[] { new CustomPopupPlacement(new Point(offsetX, newY), PopupPrimaryAxis.None) };
@@ -172,6 +213,7 @@ private CustomPopupPlacement[] ComboBoxCustomPopupPlacementCallback(Size popupSi
172213
{
173214
SetChildTemplateIfNeed(DownContentTemplate);
174215

216+
var downVerticalOffsetIndepent = DpiHelper.TransformToDeviceY(mainVisual, DownVerticalOffset);
175217
var newY = downVerticalOffsetIndepent;
176218

177219
return new[] { new CustomPopupPlacement(new Point(offsetX, newY), PopupPrimaryAxis.None) };

MaterialDesignThemes.Wpf/Converters/BrushRoundConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class BrushRoundConverter : IValueConverter
1515
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
1616
{
1717
var solidColorBrush = value as SolidColorBrush;
18-
if (solidColorBrush == null) return Binding.DoNothing;
18+
if (solidColorBrush == null) return null;
1919

2020
var color = solidColorBrush.Color;
2121

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.ComboBox.xaml

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
<converters:TextFieldHintVisibilityConverter x:Key="TextFieldHintVisibilityConverter" IsNotEmptyValue="Collapsed" />
1313
<converters:MathConverter x:Key="MathAddConverter" Operation="Add" />
1414
<converters:BrushRoundConverter x:Key="BrushRoundConverter" />
15+
16+
<system:Double x:Key="PopupContentPresenterExtend">4</system:Double>
17+
<system:Double x:Key="PopupTopBottomMargin">8</system:Double>
18+
<system:Double x:Key="PopupLeftRightMargin">16</system:Double>
1519

1620
<Style x:Key="FocusVisual">
1721
<Setter Property="Control.Template">
@@ -27,10 +31,6 @@
2731
</Setter>
2832
</Style>
2933

30-
<system:Double x:Key="PopupContentPresenterExtend">4</system:Double>
31-
<system:Double x:Key="PopupTopBottomMargin">8</system:Double>
32-
<system:Double x:Key="PopupLeftRightMargin">16</system:Double>
33-
3434
<ControlTemplate x:Key="PopupContentUpTemplate" TargetType="ContentControl">
3535
<Grid MinWidth="{Binding ElementName=templateRoot, Path=ActualWidth, Converter={StaticResource MathAddConverter}, ConverterParameter=32}"
3636
Margin="6">
@@ -55,34 +55,34 @@
5555
</Grid.RowDefinitions>
5656
<Border Grid.Row="0"
5757
CornerRadius="2 2 0 0"
58-
Background="{Binding ElementName=templateRoot, Path=Background}"
58+
Background="{Binding ElementName=PART_Popup, Path=Background}"
5959
Height="{StaticResource PopupTopBottomMargin}"/>
6060
<ContentPresenter Grid.Row="1"/>
61-
<Rectangle Grid.Row="2"
62-
Fill="{Binding ElementName=templateRoot, Path=Background}"
63-
Height="{StaticResource PopupContentPresenterExtend}"/>
61+
<Border Grid.Row="2"
62+
Background="{Binding ElementName=PART_Popup, Path=Background}"
63+
Height="{StaticResource PopupContentPresenterExtend}"/>
6464

6565
<Grid Grid.Row="3">
6666
<Grid.ColumnDefinitions>
6767
<ColumnDefinition Width="Auto"/>
6868
<ColumnDefinition Width="Auto"/>
6969
<ColumnDefinition Width="*"/>
7070
</Grid.ColumnDefinitions>
71-
<Rectangle Grid.Column="0"
72-
Width="{StaticResource PopupLeftRightMargin}"
73-
Fill="{Binding ElementName=templateRoot, Path=Background}"/>
71+
<Border Grid.Column="0"
72+
Width="{StaticResource PopupLeftRightMargin}"
73+
Background="{Binding ElementName=PART_Popup, Path=Background}"/>
7474
<Grid Grid.Column="1"
7575
Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type wpf:ComboBoxPopup}}, Path=VisiblePlacementWidth}"
7676
Height="{Binding ElementName=templateRoot, Path=ActualHeight}"/>
77-
<Rectangle Grid.Column="2"
78-
MinWidth="{StaticResource PopupLeftRightMargin}"
79-
Fill="{Binding ElementName=templateRoot, Path=Background}"/>
77+
<Border Grid.Column="2"
78+
MinWidth="{StaticResource PopupLeftRightMargin}"
79+
Background="{Binding ElementName=PART_Popup, Path=Background}"/>
8080
</Grid>
8181

8282
<Border Grid.Row="4"
8383
CornerRadius="0 0 2 2"
8484
Height="{StaticResource PopupTopBottomMargin}"
85-
Background="{Binding ElementName=templateRoot, Path=Background}" />
85+
Background="{Binding ElementName=PART_Popup, Path=Background}" />
8686
</Grid>
8787
</Grid>
8888
</ControlTemplate>
@@ -112,7 +112,7 @@
112112
</Grid.RowDefinitions>
113113
<Border Grid.Row="0"
114114
CornerRadius="2 2 0 0"
115-
Background="{Binding ElementName=templateRoot, Path=Background}"
115+
Background="{Binding ElementName=PART_Popup, Path=Background}"
116116
Height="{StaticResource PopupTopBottomMargin}"/>
117117

118118
<Grid Grid.Row="1">
@@ -121,29 +121,29 @@
121121
<ColumnDefinition Width="Auto"/>
122122
<ColumnDefinition Width="*"/>
123123
</Grid.ColumnDefinitions>
124-
<Rectangle Grid.Column="0"
125-
Width="{StaticResource PopupLeftRightMargin}"
126-
Fill="{Binding ElementName=templateRoot, Path=Background}"
124+
<Border Grid.Column="0"
125+
Width="{StaticResource PopupLeftRightMargin}"
126+
Background="{Binding ElementName=PART_Popup, Path=Background}"
127127
/>
128128
<Grid Grid.Column="1"
129129
Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type wpf:ComboBoxPopup}}, Path=VisiblePlacementWidth}"
130130
Height="{Binding ElementName=templateRoot, Path=ActualHeight}"/>
131-
<Rectangle Grid.Column="2"
132-
MinWidth="{StaticResource PopupLeftRightMargin}"
133-
Fill="{Binding ElementName=templateRoot, Path=Background}"
131+
<Border Grid.Column="2"
132+
MinWidth="{StaticResource PopupLeftRightMargin}"
133+
Background="{Binding ElementName=PART_Popup, Path=Background}"
134134
/>
135135
</Grid>
136136

137-
<Rectangle Grid.Row="2"
138-
Fill="{Binding ElementName=templateRoot, Path=Background}"
139-
Height="{StaticResource PopupContentPresenterExtend}"/>
137+
<Border Grid.Row="2"
138+
Background="{Binding ElementName=PART_Popup, Path=Background}"
139+
Height="{StaticResource PopupContentPresenterExtend}"/>
140140

141141
<ContentPresenter Grid.Row="3"/>
142142

143143
<Border Grid.Row="4"
144144
CornerRadius="0 0 2 2"
145145
Height="{StaticResource PopupTopBottomMargin}"
146-
Background="{Binding ElementName=templateRoot, Path=Background}" />
146+
Background="{Binding ElementName=PART_Popup, Path=Background}" />
147147
</Grid>
148148
</Grid>
149149
</ControlTemplate>
@@ -170,15 +170,15 @@
170170
</Grid.RowDefinitions>
171171
<Border Grid.Row="0"
172172
CornerRadius="2 2 0 0"
173-
Background="{Binding ElementName=templateRoot, Path=Background}"
174-
Height="{StaticResource PopupTopBottomMargin}"/>
173+
Background="{Binding ElementName=PART_Popup, Path=Background}"
174+
Height="{StaticResource PopupTopBottomMargin}"/>
175175

176176
<ContentPresenter Grid.Row="1"/>
177177

178178
<Border Grid.Row="2"
179179
CornerRadius="0 0 2 2"
180180
Height="{StaticResource PopupTopBottomMargin}"
181-
Background="{Binding ElementName=templateRoot, Path=Background}" />
181+
Background="{Binding ElementName=PART_Popup, Path=Background}" />
182182
</Grid>
183183
</Grid>
184184
</ControlTemplate>
@@ -441,25 +441,25 @@
441441
Visibility="{Binding Path=(wpf:TextFieldAssist.DecorationVisibility), RelativeSource={RelativeSource TemplatedParent}}"/>
442442

443443
<wpf:ComboBoxPopup x:Name="PART_Popup"
444-
AllowsTransparency="true"
445-
Focusable="False"
446-
HorizontalOffset="-11.5"
447-
IsOpen="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
448-
PlacementTarget="{Binding ElementName=templateRoot}"
449-
SnapsToDevicePixels="True"
450-
UseLayoutRounding="True"
451-
Placement="Custom"
452-
PopupAnimation="Fade"
453-
VerticalOffset="0"
454-
DefaultVerticalOffset="5"
455-
DownVerticalOffset="-15.5"
456-
UpVerticalOffset="15"
457-
UpContentTemplate="{StaticResource PopupContentUpTemplate}"
458-
DownContentTemplate="{StaticResource PopupContentDownTemplate}"
459-
DefaultContentTemplate="{StaticResource PopupContentDefaultTemplate}">
444+
AllowsTransparency="true"
445+
Focusable="False"
446+
HorizontalOffset="-11.5"
447+
IsOpen="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
448+
PlacementTarget="{Binding ElementName=templateRoot}"
449+
SnapsToDevicePixels="True"
450+
UseLayoutRounding="True"
451+
Placement="Custom"
452+
PopupAnimation="Fade"
453+
VerticalOffset="0"
454+
DefaultVerticalOffset="5"
455+
DownVerticalOffset="-15.5"
456+
UpVerticalOffset="15"
457+
UpContentTemplate="{StaticResource PopupContentUpTemplate}"
458+
DownContentTemplate="{StaticResource PopupContentDownTemplate}"
459+
DefaultContentTemplate="{StaticResource PopupContentDefaultTemplate}">
460460
<ContentControl>
461461
<ScrollViewer MaxHeight="{TemplateBinding MaxDropDownHeight}"
462-
Background="{TemplateBinding Background}">
462+
Background="{Binding Background, ElementName=PART_Popup}">
463463
<ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" />
464464
</ScrollViewer>
465465
</ContentControl>
@@ -525,7 +525,7 @@
525525

526526
<Style x:Key="MaterialDesignComboBox" TargetType="{x:Type ComboBox}">
527527
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
528-
<Setter Property="Background" Value="{DynamicResource MaterialDesignPaper}"/>
528+
<Setter Property="Background" Value="Transparent"/>
529529
<Setter Property="BorderBrush" Value="{DynamicResource MaterialDesignTextBoxBorder}"/>
530530
<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}"/>
531531
<Setter Property="BorderThickness" Value="0 0 0 1"/>

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.ScrollBar.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
<Style x:Key="MaterialDesignScrollBar" TargetType="{x:Type ScrollBar}">
8888
<Setter Property="Stylus.IsPressAndHoldEnabled" Value="false"/>
8989
<Setter Property="Stylus.IsFlicksEnabled" Value="false"/>
90-
<Setter Property="Background" Value="{DynamicResource MaterialDesignPaper}"/>
90+
<Setter Property="Background" Value="Transparent"/>
9191
<Setter Property="BorderBrush" Value="Transparent"/>
9292
<Setter Property="Foreground" Value="{DynamicResource MaterialDesignSelection}"/>
9393
<Setter Property="BorderThickness" Value="1,0"/>

MaterialDesignThemes.Wpf/TreeHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static double GetVisibleWidth(FrameworkElement element, UIElement parent)
2727
return width;
2828
}
2929

30-
//BinarySearch there
30+
//BinarySearch here
3131
int end = (int) Math.Floor(element.ActualWidth);
3232
int start = 0;
3333

0 commit comments

Comments
 (0)