WPF 如何模板化?

许晋升 2019-06-24 12:00:10

<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>

各位大神,上面的Button要有许多个,其中***每一个都不一样,怎么做才能减少代码量。
...全文
196 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hez2010 2019-06-26
  • 打赏
  • 举报
回复
使用 DataTemplate 啊
货郎大叔 2019-06-25
  • 打赏
  • 举报
回复
是指的样式不一样吗,如果是的话,那只有一个一个写
许晋升 2019-06-25
  • 打赏
  • 举报
回复
额,我还是一个一个写吧
desperaso 2019-06-24
  • 打赏
  • 举报
回复
只是提供个思路

/// <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;
}

许晋升 2019-06-24
  • 打赏
  • 举报
回复
<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"/>

我的意思是这两个Button怎么模板化
IT流水兵 2019-06-24
  • 打赏
  • 举报
回复
  <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>
exception92 2019-06-24
  • 打赏
  • 举报
回复
命令最终调用某个方法,你可以把所有button都指向同一个命令,然后通过commandparameter来区分invoke的方法逻辑即可。

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧