调整demo的combobox样式

This commit is contained in:
艾竹
2022-12-01 23:12:13 +08:00
parent 992a4bb83b
commit 503b92f40e
9 changed files with 412 additions and 159 deletions

View File

@@ -0,0 +1,94 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="ComboBoxStyle" TargetType="ComboBox">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
<Setter Property="IsSynchronizedWithCurrentItem" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<Grid>
<ToggleButton x:Name="ToggleButton" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"
Grid.Column="2" IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
Focusable="false"
ClickMode="Press" HorizontalContentAlignment="Left" >
<ToggleButton.Template>
<ControlTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="12"/>
</Grid.ColumnDefinitions>
<Border
x:Name="Border"
Grid.ColumnSpan="2"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}" />
<Border
x:Name="BorderComp"
Grid.Column="0"
CornerRadius="2"
Margin="1"
BorderThickness="0,0,0,0" >
<TextBlock Text="{Binding Path=Text,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ComboBox}}" Padding="3" />
</Border>
<Border x:Name="ArrowBorder" Grid.Column="1" >
<Path
x:Name="Arrow"
Stretch="Fill" Width="5" Height="3"
Fill="Black"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Data="M 0 0 L 4 4 L 8 0 Z"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="ArrowBorder" Property="Background" Value="{DynamicResource Fluent.Ribbon.Brushes.Button.MouseOver.Background}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ToggleButton.Template>
</ToggleButton>
<Popup x:Name="Popup"
Placement="Bottom"
AllowsTransparency="True"
Focusable="False" IsOpen="{TemplateBinding IsDropDownOpen}"
PopupAnimation="Slide">
<Grid x:Name="DropDown"
SnapsToDevicePixels="True"
MinWidth="{TemplateBinding ActualWidth}"
MaxHeight="{TemplateBinding MaxDropDownHeight}">
<Border x:Name="DropDownBorder"
BorderThickness="1" Background="{DynamicResource WhiteBrush}"
BorderBrush="{DynamicResource GrayBrush8}"/>
<ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True" DataContext="{Binding}">
<StackPanel Background="AliceBlue" IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
</ScrollViewer>
</Grid>
</Popup>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="HasItems" Value="false">
<Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
</Trigger>
<Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
<Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0"/>
</Trigger>
<Trigger Property="IsDropDownOpen" Value="true">
<Setter TargetName="ToggleButton" Property="Background" Value="{DynamicResource Fluent.Ribbon.Brushes.Button.MouseOver.Background}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>

View File

@@ -8,14 +8,18 @@
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.CanContentScroll="True"
IsSynchronizedWithCurrentItem="True"
>
<ComboBox.ItemTemplate>
IsSynchronizedWithCurrentItem="True">
<ComboBox.Resources>
<Style TargetType="ComboBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ComboBox.Resources>
<ComboBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Title}"
IsChecked="{Binding Path=IsSelected, Mode=TwoWay}"
Tag="{RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}"
Click="CheckBox_Click" />
Tag="{RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}"
Click="CheckBox_Click" />
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.Template>
@@ -63,8 +67,8 @@
</ControlTemplate.Triggers>
</ControlTemplate>
</ToggleButton.Template>
</ToggleButton>
<Popup x:Name="Popup"
</ToggleButton>
<Popup x:Name="Popup"
Placement="Bottom"
AllowsTransparency="True"
Focusable="False" IsOpen="{TemplateBinding IsDropDownOpen}"

View File

@@ -34,22 +34,22 @@ namespace AIStudio.Wpf.DiagramHelper.Controls
#region Dependency Properties
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(IList), typeof(MultiSelectComboBox), new FrameworkPropertyMetadata(null,
DependencyProperty.Register(nameof(ItemsSource), typeof(IList), typeof(MultiSelectComboBox), new FrameworkPropertyMetadata(null,
new PropertyChangedCallback(MultiSelectComboBox.OnItemsSourceChanged)));
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.Register("SelectedItems", typeof(IList), typeof(MultiSelectComboBox), new FrameworkPropertyMetadata(null,
DependencyProperty.Register(nameof(SelectedItems), typeof(IList), typeof(MultiSelectComboBox), new FrameworkPropertyMetadata(null,
new PropertyChangedCallback(MultiSelectComboBox.OnSelectedItemsChanged)));
public static readonly DependencyProperty SelectedValuesProperty =
DependencyProperty.Register("SelectedValues", typeof(IList), typeof(MultiSelectComboBox), new FrameworkPropertyMetadata(null,
DependencyProperty.Register(nameof(SelectedValues), typeof(IList), typeof(MultiSelectComboBox), new FrameworkPropertyMetadata(null,
new PropertyChangedCallback(MultiSelectComboBox.OnSelectedValuesChanged)));
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(MultiSelectComboBox), new UIPropertyMetadata(string.Empty));
DependencyProperty.Register(nameof(Text), typeof(string), typeof(MultiSelectComboBox), new UIPropertyMetadata(string.Empty));
public static readonly DependencyProperty DefaultTextProperty =
DependencyProperty.Register("DefaultText", typeof(string), typeof(MultiSelectComboBox), new UIPropertyMetadata(string.Empty));
DependencyProperty.Register(nameof(DefaultText), typeof(string), typeof(MultiSelectComboBox), new UIPropertyMetadata(string.Empty));
public string DisplayMemberPath { get; set; }

View File

@@ -16,11 +16,11 @@ using System.Reflection;
namespace AIStudio.Wpf.DiagramHelper.Controls
{
/// <summary>
/// Interaction logic for PropertiesView.xaml
/// </summary>
public partial class PropertiesView : UserControl
{
/// <summary>
/// Interaction logic for PropertiesView.xaml
/// </summary>
public partial class PropertiesView : UserControl
{
#region SelectedObject
public static readonly DependencyProperty SelectedObjectProperty = DependencyProperty.Register("SelectedObject", typeof(object), typeof(PropertiesView), new UIPropertyMetadata(null, OnSelectedObjectChanged));
@@ -70,159 +70,194 @@ namespace AIStudio.Wpf.DiagramHelper.Controls
}
}
public bool CustomSetting
{
get;
set;
}
public PropertiesView()
{
InitializeComponent();
this.Loaded += PropertiesView_Loaded;
}
{
InitializeComponent();
this.Loaded += PropertiesView_Loaded;
}
private void PropertiesView_Loaded(object sender, RoutedEventArgs e)
{
DisplayProperties();
}
DisplayProperties();
}
void PropertyChanged(object sender, PropertyChangedEventArgs e)
{
DisplayProperties();
}
{
DisplayProperties();
}
private void DisplayProperties()
{
_panel.Children.Clear();
ClearGrid();
if (SelectedObject != null)
{
int row = 0;
foreach (var prop in SelectedObject.GetType().GetProperties())
{
var attr = prop.GetCustomAttributes(typeof(BrowsableAttribute), true);
if (NeedBrowsable == false && (attr.Length == 0 || (attr[0] as BrowsableAttribute).Browsable))
{
DisplayProperty(prop, row);
row++;
}
else if(NeedBrowsable == true && (attr.Length > 0 && (attr[0] as BrowsableAttribute).Browsable))
private void DisplayProperties()
{
_panel.Children.Clear();
ClearGrid();
if (SelectedObject != null)
{
if (CustomSetting == true)
{
if (SelectedObject.GetType().GetProperty("PropertiesSetting") != null)
{
DisplayProperty(prop, row);
row++;
Dictionary<string, string> settings = SelectedObject.GetType().GetProperty("PropertiesSetting").GetValue(SelectedObject) as Dictionary<string, string>;
int row = 0;
foreach (var setting in settings)
{
var prop = SelectedObject.GetType().GetProperty(setting.Key);
DisplayProperty(prop, row, setting.Value);
row++;
}
}
}
_panel.Children.Add(_gridContainer);
}
else
{
_panel.Children.Add(_label);
}
}
else
{
int row = 0;
foreach (var prop in SelectedObject.GetType().GetProperties())
{
var attr = prop.GetCustomAttributes(typeof(BrowsableAttribute), true);
if (NeedBrowsable == false && (attr.Length == 0 || (attr[0] as BrowsableAttribute).Browsable))
{
DisplayProperty(prop, row);
row++;
}
else if (NeedBrowsable == true && (attr.Length > 0 && (attr[0] as BrowsableAttribute).Browsable))
{
DisplayProperty(prop, row);
row++;
}
}
}
_panel.Children.Add(_gridContainer);
}
else
{
_panel.Children.Add(_label);
}
}
private void ClearGrid()
{
_grid.RowDefinitions.Clear();
for (int i = _grid.Children.Count - 1; i >= 0; i--)
{
if (_grid.Children[i] != _vLine && _grid.Children[i] != _splitter)
_grid.Children.RemoveAt(i);
}
}
private void ClearGrid()
{
_grid.RowDefinitions.Clear();
for (int i = _grid.Children.Count - 1; i >= 0; i--)
{
if (_grid.Children[i] != _vLine && _grid.Children[i] != _splitter)
_grid.Children.RemoveAt(i);
}
}
private void DisplayProperty(PropertyInfo prop, int row)
{
var rowDef = new RowDefinition();
rowDef.Height = new GridLength(Math.Max(20, this.FontSize * 2));
_grid.RowDefinitions.Add(rowDef);
private void DisplayProperty(PropertyInfo prop, int row, string name = null)
{
var rowDef = new RowDefinition();
rowDef.Height = new GridLength(Math.Max(20, this.FontSize * 2));
_grid.RowDefinitions.Add(rowDef);
var tb = new TextBlock() { Text = prop.Name };
var displayAttr = prop.GetCustomAttributes(typeof(DisplayNameAttribute), true);
if (displayAttr.Length > 0 )
if (name != null)
{
tb.Text = (displayAttr[0] as DisplayNameAttribute).DisplayName;
tb.Text = name;
tb.ToolTip = prop.Name;
}
else
{
var displayAttr = prop.GetCustomAttributes(typeof(DisplayNameAttribute), true);
if (displayAttr.Length > 0)
{
tb.Text = (displayAttr[0] as DisplayNameAttribute).DisplayName;
tb.ToolTip = prop.Name;
}
}
tb.Margin = new Thickness(4);
tb.VerticalAlignment = VerticalAlignment.Center;
Grid.SetColumn(tb, 0);
Grid.SetRow(tb, _grid.RowDefinitions.Count - 1);
_grid.Children.Add(tb);
Grid.SetColumn(tb, 0);
Grid.SetRow(tb, _grid.RowDefinitions.Count - 1);
_grid.Children.Add(tb);
var line = new Line();
line.Style = (Style)border.Resources["gridHorizontalLineStyle"];
Grid.SetRow(line, row);
_grid.Children.Add(line);
var line = new Line();
line.Style = (Style)border.Resources["gridHorizontalLineStyle"];
Grid.SetRow(line, row);
_grid.Children.Add(line);
Style style = null;
var styleNameAttr = prop.GetCustomAttributes(typeof(StyleNameAttribute), true);
if (styleNameAttr.Length > 0)
{
style = this.FindResource((styleNameAttr[0] as StyleNameAttribute).Name) as Style;
if (style != null)
{
ContentControl content = new ContentControl();
content.Style = style;
content.DataContext = SelectedObject;
Style style = null;
var styleNameAttr = prop.GetCustomAttributes(typeof(StyleNameAttribute), true);
if (styleNameAttr.Length > 0)
{
style = this.FindResource((styleNameAttr[0] as StyleNameAttribute).Name) as Style;
if (style != null)
{
ContentControl content = new ContentControl();
content.Style = style;
content.DataContext = SelectedObject;
Grid.SetColumn(content, 1);
Grid.SetRow(content, _grid.RowDefinitions.Count - 1);
Grid.SetColumn(content, 1);
Grid.SetRow(content, _grid.RowDefinitions.Count - 1);
_grid.Children.Add(content);
}
}
if (style == null)
{
var ed = new TextBox();
ed.PreviewKeyDown += new KeyEventHandler(ed_KeyDown);
ed.Margin = new Thickness(0);
ed.VerticalAlignment = VerticalAlignment.Center;
ed.BorderThickness = new Thickness(0);
Grid.SetColumn(ed, 1);
Grid.SetRow(ed, _grid.RowDefinitions.Count - 1);
_grid.Children.Add(content);
}
}
var binding = new Binding(prop.Name);
binding.Source = SelectedObject;
binding.ValidatesOnExceptions = true;
binding.Mode = BindingMode.OneWay;
if (prop.CanWrite)
{
var mi = prop.GetSetMethod();
if (mi != null && mi.IsPublic)
binding.Mode = BindingMode.TwoWay;
}
ed.SetBinding(TextBox.TextProperty, binding);
if (style == null)
{
var ed = new TextBox();
ed.PreviewKeyDown += new KeyEventHandler(ed_KeyDown);
ed.Margin = new Thickness(0);
ed.VerticalAlignment = VerticalAlignment.Center;
ed.BorderThickness = new Thickness(0);
Grid.SetColumn(ed, 1);
Grid.SetRow(ed, _grid.RowDefinitions.Count - 1);
var template = (ControlTemplate)border.Resources["validationErrorTemplate"];
Validation.SetErrorTemplate(ed, template);
var binding = new Binding(prop.Name);
binding.Source = SelectedObject;
binding.ValidatesOnExceptions = true;
binding.Mode = BindingMode.OneWay;
if (prop.CanWrite)
{
var mi = prop.GetSetMethod();
if (mi != null && mi.IsPublic)
binding.Mode = BindingMode.TwoWay;
}
ed.SetBinding(TextBox.TextProperty, binding);
_grid.Children.Add(ed);
}
}
var template = (ControlTemplate)border.Resources["validationErrorTemplate"];
Validation.SetErrorTemplate(ed, template);
void ed_KeyDown(object sender, KeyEventArgs e)
{
var ed = sender as TextBox;
if (ed != null)
{
if (e.Key == Key.Enter)
{
ed.GetBindingExpression(TextBox.TextProperty).UpdateSource();
e.Handled = true;
}
else if (e.Key == Key.Escape)
ed.GetBindingExpression(TextBox.TextProperty).UpdateTarget();
}
}
}
_grid.Children.Add(ed);
}
}
[AttributeUsage(AttributeTargets.Property)]
public class StyleNameAttribute : Attribute
{
private string _name;
public StyleNameAttribute(string name)
{
_name = name;
}
void ed_KeyDown(object sender, KeyEventArgs e)
{
var ed = sender as TextBox;
if (ed != null)
{
if (e.Key == Key.Enter)
{
ed.GetBindingExpression(TextBox.TextProperty).UpdateSource();
e.Handled = true;
}
else if (e.Key == Key.Escape)
ed.GetBindingExpression(TextBox.TextProperty).UpdateTarget();
}
}
}
public string Name
{
get { return _name; }
}
}
[AttributeUsage(AttributeTargets.Property)]
public class StyleNameAttribute : Attribute
{
private string _name;
public StyleNameAttribute(string name)
{
_name = name;
}
public string Name
{
get
{
return _name;
}
}
}
}