110,533
社区成员
发帖
与我相关
我的任务
分享
<StackPanel Height="100">
<Button Command="***" Height="100" Width="100">
<materialDesign:PackIcon Kind="***" HorizontalAlignment="Center" VerticalAlignment="Center" Height="100" Width="100"/>
<Button Command="***" Height="100" Width="100">
<materialDesign:PackIcon Kind="***" HorizontalAlignment="Center" VerticalAlignment="Center" Height="100" Width="100"/>
</StackPanel>
/// <param name="StyleName">样式名称</param>
/// <param name="BorderColor">初始边框色</param>
/// <param name="MouseOverColor">鼠标离开边框色</param>
/// <param name="FocuseColor">焦点边框色</param>
/// <param name="TextPading">文字距离边框</param>
/// <param name="Radius">圆角度数</param>
/// <param name="GradientBegin">背景渐变起始色</param>
/// <param name="GradientEnd">背景渐变结束色</param>
/// <returns></returns>
public void DynamicStyle (
TextBox textBox,
string StyleName,
string BorderColor, string MouseOverColor, string FocuseColor,
string TextPading, int Radius,
string GradientBegin, string GradientEnd)
{
Resources.Remove(StyleName);
string _style = @"
<ResourceDictionary xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<SolidColorBrush x:Key='TextBox.Static.Border' Color='"+ BorderColor + @"'/>
<SolidColorBrush x:Key='TextBox.MouseOver.Border' Color='"+ MouseOverColor + @"'/>
<SolidColorBrush x:Key='TextBox.Focus.Border' Color='"+ FocuseColor + @"'/>
<Style x:Key='"+ StyleName + @"' TargetType='{x:Type TextBox}'>
<Setter Property='Background' Value='{DynamicResource {x:Static SystemColors.WindowBrushKey}}'/>
<Setter Property='BorderBrush' Value='{StaticResource TextBox.Static.Border}'/>
<Setter Property='Foreground' Value='{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}'/>
<Setter Property='BorderThickness' Value='1'/>
<Setter Property='KeyboardNavigation.TabNavigation' Value='None'/>
<Setter Property='HorizontalContentAlignment' Value='Left'/>
<Setter Property='VerticalContentAlignment' Value='Center'/>
<Setter Property='FocusVisualStyle' Value='{x:Null}'/>
<Setter Property='AllowDrop' Value='true'/>
<Setter Property='ScrollViewer.PanningMode' Value='VerticalFirst'/>
<Setter Property='Stylus.IsFlicksEnabled' Value='False'/>
<Setter Property='Padding' Value='"+ TextPading + @"'/>
<Setter Property='Template'>
<Setter.Value>
<ControlTemplate TargetType='{x:Type TextBox}'>
<Border x:Name='border' BorderBrush='{TemplateBinding BorderBrush}' BorderThickness='{TemplateBinding BorderThickness}' SnapsToDevicePixels='True' CornerRadius='"+ Radius + @"'>
<Border.Background>
<LinearGradientBrush EndPoint='0.5,1' StartPoint='0.5,0'>
<GradientStop Color='"+ GradientBegin + @"' Offset='0'/>
<GradientStop Color='"+ GradientEnd + @"' Offset='1'/>
</LinearGradientBrush>
</Border.Background>
<ScrollViewer x:Name='PART_ContentHost' Focusable='false' HorizontalScrollBarVisibility='Hidden' VerticalScrollBarVisibility='Hidden'></ScrollViewer>
</Border>
<ControlTemplate.Triggers>
<Trigger Property='IsEnabled' Value='false'>
<Setter Property='Opacity' TargetName='border' Value='0.56'/>
</Trigger>
<Trigger Property='IsMouseOver' Value='true'>
<Setter Property='BorderBrush' TargetName='border' Value='{StaticResource TextBox.MouseOver.Border}'/>
</Trigger>
<Trigger Property='IsKeyboardFocused' Value='true'>
<Setter Property='BorderBrush' TargetName='border' Value='{StaticResource TextBox.Focus.Border}'/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property='IsInactiveSelectionHighlightEnabled' Value='true'/>
<Condition Property='IsSelectionActive' Value='false'/>
</MultiTrigger.Conditions>
<Setter Property='SelectionBrush' Value='{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}'/>
</MultiTrigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
";
StringReader strreader = new StringReader(_style);
XmlTextReader xmlreader = new XmlTextReader(strreader);
object obj = XamlReader.Load(xmlreader);
ResourceDictionary _ControlStyle = (ResourceDictionary)obj;
Resources.MergedDictionaries.Add(_ControlStyle);
textBox.Style = Resources[StyleName] as Style;
}
<Button Command="Command1" Height="100" Width="100">
<materialDesign:PackIcon Kind="icon1*" HorizontalAlignment="Center" VerticalAlignment="Center" Height="100" Width="100"/>
<Button Command="Command2" Height="100" Width="100">
<materialDesign:PackIcon Kind="icon2" HorizontalAlignment="Center" VerticalAlignment="Center" Height="100" Width="100"/>
<StackPanel Height="200">
<StackPanel.Resources>
<Style TargetType="Button">
<Setter Property="Height" Value="100"/>
<Setter Property="Width" Value="100"/>
</Style>
</StackPanel.Resources>
<Button Command="" />
<Button Command=""/>
......
</StackPanel>