原文: Bootstrap WPF Style,Bootstrap风格的WPF样式
简介
GitHub地址:https://github.com/ptddqr/bootstrap-wpf-style
此样式基于bootstrap-3.3.0,样式文件里的源码行数都是指的这个版本.CSS源文件放到了Content文件夹下的bootstrap.css
WPF样式和CSS还是不太相同,所以有些内容实现上稍有出入,有些内容用法不太一样,有些内容并没有实现
但至少,一些概念,尺寸和取色,还是很好的借鉴
博客说明按Bootstrap官方文档的顺序来写
App.xaml里引用Bootstrap.xaml资源
11 <Application.Resources> 22 <ResourceDictionary> 33 <ResourceDictionary.MergedDictionaries> 44 <ResourceDictionary Source="pack://application:,,,/BootstrapWpfStyle;component/Styles/Bootstrap.xaml"/> 55 </ResourceDictionary.MergedDictionaries> 66 </ResourceDictionary> 77 </Application.Resources>
排版
标题
<Label>样式 h1到h6
效果

代码
11 <Label Content="h1. Bootstrap heading" Style="{DynamicResource h1}"></Label> 22 <Label Content="h2. Bootstrap heading" Style="{DynamicResource h2}"></Label> 33 <Label Content="h3. Bootstrap heading" Style="{DynamicResource h3}"></Label> 44 <Label Content="h4. Bootstrap heading" Style="{DynamicResource h4}"></Label> 55 <Label Content="h5. Bootstrap heading" Style="{DynamicResource h5}"></Label> 66 <Label Content="h6. Bootstrap heading" Style="{DynamicResource h6}"></Label>
副标题
<Label>样式 h1 small到h6 small.使用时 和标题放到一个<StackPanel>里 方便对齐
效果

代码
1 1 <StackPanel Orientation="Horizontal"> 2 2 <Label Content="h1. Bootstrap heading" Style="{DynamicResource h1}"></Label> 3 3 <Label Content="Secondary text" Style="{DynamicResource h1 small}"></Label> 4 4 </StackPanel> 5 5 <StackPanel Orientation="Horizontal"> 6 6 <Label Content="h2. Bootstrap heading" Style="{DynamicResource h2}"></Label> 7 7 <Label Content="Secondary text" Style="{DynamicResource h2 small}"></Label> 8 8 </StackPanel> 9 9 <StackPanel Orientation="Horizontal"> 1010 <Label Content="h3. Bootstrap heading" Style="{DynamicResource h3}"></Label> 1111 <Label Content="Secondary text" Style="{DynamicResource h3 small}"></Label> 1212 </StackPanel> 1313 <StackPanel Orientation="Horizontal"> 1414 <Label Content="h4. Bootstrap heading" Style="{DynamicResource h4}"></Label> 1515 <Label Content="Secondary text" Style="{DynamicResource h4 small}"></Label> 1616 </StackPanel> 1717 <StackPanel Orientation="Horizontal"> 1818 <Label Content="h5. Bootstrap heading" Style="{DynamicResource h5}"></Label> 1919 <Label Content="Secondary text" Style="{DynamicResource h5 small}"></Label> 2020 </StackPanel> 2121 <StackPanel Orientation="Horizontal"> 2222 <Label Content="h6. Bootstrap heading" Style="{DynamicResource h6}"></Label> 2323 <Label Content="Secondary text" Style="{DynamicResource h6 small}"></Label> 2424 </StackPanel>
代码
内联代码
<Label>样式 code.使用时 和其他<Label>放到一个<StackPanel>里
效果

代码
11 <StackPanel Orientation="Horizontal"> 22 <Label Content="内联代码:For example, "></Label> 33 <Label Content="<section>" Style="{DynamicResource code}"></Label> 44 <Label Content=" should be wrapped as inline."></Label> 55 </StackPanel>
用户输入
<Label>样式 kbd.使用时 和其他<Label>放到一个<StackPanel>里
效果

代码
11 <StackPanel Orientation="Horizontal"> 22 <Label Content="用户输入:To edit settings, press "></Label> 33 <Label Content="ctrl + ," Style="{DynamicResource kbd}"></Label> 44 </StackPanel>
代码块
<Label>样式 pre
效果

代码
11 <Label xml:space="preserve" Style="{DynamicResource pre}">代码块: 22 Foreground:#333 33 Background:#f5f5f5 44 BorderBrush:#ccc</Label>
辅助文本
<Label>样式 help-block.注:辅助文本在Bootstrap表单样式里,原本的解释:针对表单控件的“块(block)”级辅助文本.我并没有建一个表单窗体,所以就写在了这里
效果

代码
1 <Label Content="辅助文本:上面为代码块" Style="{DynamicResource help-block}"></Label>
表格
<DataGrid>样式 默认就这一组样式,不用引用
效果

代码
11 <DataGrid AutoGenerateColumns="False" DataContext="{Binding Source={StaticResource SampleDataSource}}" ItemsSource="{Binding Collection}"> 22 <DataGrid.Columns> 33 <DataGridTextColumn Binding="{Binding Property1}" Header="Property1" ElementStyle="{StaticResource DataGridTextCenter}"/> 44 <DataGridTextColumn Binding="{Binding Property2}" Header="Property2" ElementStyle="{StaticResource DataGridTextCenter}"/> 55 <DataGridTextColumn Binding="{Binding Property3}" Header="Property3" ElementStyle="{StaticResource DataGridTextCenter}"/> 66 </DataGrid.Columns> 77 </DataGrid>
表单
WPF没有表单的概念,所以说明就不按官方文档的顺序了
文本输入框
<TextBox>样式 控件尺寸只支持默认样式
效果

代码
11 <TextBox Text="TextBox"/> 22 <TextBox Text="TextBox IsReadOnly=True" IsReadOnly="True"/> 33 <TextBox Text="TextBox IsEnabled=False" IsEnabled="False"/> 44 <TextBox Text="TextBox has-success" Style="{StaticResource has-success}"/> 55 <TextBox Text="TextBox has-warning" Style="{StaticResource has-warning}"/> 66 <TextBox Text="TextBox has-error" Style="{StaticResource has-error}"/> 77 <TextBox Text="TextBox input-sm" Style="{StaticResource input-sm}"/> 88 <TextBox Text="TextBox input-lg" Style="{StaticResource input-lg}"/>
密码框
<PasswordBox>样式 控件尺寸只支持默认样式
效果

代码
11 <PasswordBox Password="PasswordBox"/> 22 <PasswordBox Password="PasswordBox IsEnabled=False" IsEnabled="False"/> 33 <PasswordBox Password="PasswordBox has-success" Style="{StaticResource has-success}"/> 44 <PasswordBox Password="PasswordBox has-warning" Style="{StaticResource has-warning}"/> 55 <PasswordBox Password="PasswordBox has-error" Style="{StaticResource has-error}"/> 66 <PasswordBox Password="PasswordBox input-sm" Style="{StaticResource input-sm}"/> 77 <PasswordBox Password="PasswordBox input-lg" Style="{StaticResource input-lg}"/>
复选框
<CheckBox>样式 checkbox 和Bootstrap有些不同 自己写的样式
效果

代码
11 <CheckBox Content="default"></CheckBox> 22 <CheckBox Content="checkbox has-success" Style="{DynamicResource checkbox has-success}" IsChecked="{x:Null}"></CheckBox> 33 <CheckBox Content="checkbox has-warning" Style="{DynamicResource checkbox has-warning}" IsChecked="False"></CheckBox> 44 <CheckBox Content="checkbox has-error" Style="{DynamicResource checkbox has-error}" IsChecked="True"></CheckBox>
单选框
<RadioButton>样式 radio 和Bootstrap有些不同 自己写的样式
效果

代码
11 <RadioButton Content="default"></RadioButton> 22 <RadioButton Content="radio has-success" Style="{DynamicResource radio has-success}"></RadioButton> 33 <RadioButton Content="radio has-warning" Style="{DynamicResource radio has-warning}"></RadioButton> 44 <RadioButton Content="radio has-error" Style="{DynamicResource radio has-error}" IsChecked="True"></RadioButton>
下拉框
<ComboBox>样式 默认就这一组样式,不用引用 gif截图的原因,看不到下拉
效果

代码
1 1 <ComboBox> 2 2 <ComboBoxItem>苹果</ComboBoxItem> 3 3 <ComboBoxItem>橡胶</ComboBoxItem> 4 4 <ComboBoxItem>桔子</ComboBoxItem> 5 5 </ComboBox> 6 6 <ComboBox IsEditable="True"> 7 7 <ComboBoxItem>苹果</ComboBoxItem> 8 8 <ComboBoxItem>橡胶</ComboBoxItem> 9 9 <ComboBoxItem>桔子</ComboBoxItem> 1010 </ComboBox> 1111 <ComboBox IsEnabled="False"> 1212 <ComboBoxItem>苹果</ComboBoxItem> 1313 <ComboBoxItem>橡胶</ComboBoxItem> 1414 <ComboBoxItem>桔子</ComboBoxItem> 1515 </ComboBox>
按钮
按钮
<Button>样式 btn
效果

代码
11 <Button Content="default"></Button> 22 <Button Content="primary" Style="{DynamicResource btn-primary}"></Button> 33 <Button Content="success" Style="{DynamicResource btn-success}"></Button> 44 <Button Content="info" Style="{DynamicResource btn-info}"></Button> 55 <Button Content="warning" Style="{DynamicResource btn-warning}"></Button> 66 <Button Content="danger" Style="{DynamicResource btn-danger}"></Button>
切换按钮
<ToggleButton>样式 tbtn.Bootstrap中没有切换按钮,这里做成和按钮一样,按下去的效果就是按钮<Button>点击的效果
效果

代码
11 <ToggleButton Content="default"></ToggleButton> 22 <ToggleButton Content="primary" Style="{DynamicResource tbtn-primary}"></ToggleButton> 33 <ToggleButton Content="success" Style="{DynamicResource tbtn-success}"></ToggleButton> 44 <ToggleButton Content="info" Style="{DynamicResource tbtn-info}"></ToggleButton> 55 <ToggleButton Content="warning" Style="{DynamicResource tbtn-warning}"></ToggleButton> 66 <ToggleButton Content="danger" Style="{DynamicResource tbtn-danger}"></ToggleButton>
辅助类
Contextual colors
<Label>样式 text 语境
效果

代码
11 <Label Content="text-muted:提示,使用浅灰色" Style="{DynamicResource text-muted}"></Label> 22 <Label Content="text-primary:主要,使用蓝色" Style="{DynamicResource text-primary}"></Label> 33 <Label Content="text-success:成功,使用浅绿色" Style="{DynamicResource text-success}"></Label> 44 <Label Content="text-info:通知信息,使用浅蓝色" Style="{DynamicResource text-info}"></Label> 55 <Label Content="text-warning:警告,使用黄色" Style="{DynamicResource text-warning}"></Label> 66 <Label Content="text-danger:危险,使用褐色" Style="{DynamicResource text-danger}"></Label>
Contextual backgrounds
<Label>样式 text bg 语境
效果

代码
11 <Label Content="text bg-primary:主要,使用蓝色,Foreground使用白色" Style="{DynamicResource text bg-primary}"></Label> 22 <Label Content="text bg-success:成功,使用浅绿色" Style="{DynamicResource text bg-success}"></Label> 33 <Label Content="text bg-info:通知信息,使用浅蓝色" Style="{DynamicResource text bg-info}"></Label> 44 <Label Content="text bg-warning:警告,使用黄色" Style="{DynamicResource text bg-warning}"></Label> 55 <Label Content="text bg-danger:危险,使用褐色" Style="{DynamicResource text bg-danger}"></Label>
输入框组
插件
<TextBox>样式 input-group-addon 输入框里带个<Label>,其实并不是插件,addon这个单词,使用百度翻译,翻译成插件.<Label>里显示的内容绑定到Tag属性
效果

代码
11 <TextBox Text="左边带插件的输入组" Tag="@" Style="{DynamicResource input-group-addon left}"></TextBox> 22 <TextBox Text="右边带插件的输入组" Tag=".00" Style="{DynamicResource input-group-addon right}"></TextBox>
作为额外元素的按钮
<TextBox>样式 input-group-btn 输入框里带个<Button>,<Button>里显示的内容绑定到Tag属性
效果

代码
1 1 xmal代码: 2 2 <TextBox Text="左边带按钮的输入组" Tag="GO!" Style="{DynamicResource input-group-btn left}" Button.Click="InputGroupButton_Click"></TextBox> 3 3 <TextBox Text="右边带按钮的输入组" Style="{DynamicResource input-group-btn right}" Button.Click="InputGroupButton_Click"> 4 4 <TextBox.Tag> 5 5 <Path Style="{DynamicResource InputGroupPathStyle}" Data="{DynamicResource PathDataSearch}"></Path> 6 6 </TextBox.Tag> 7 7 </TextBox> 8 8 9 9 后台代码C#: 1010 private void InputGroupButton_Click(object sender, RoutedEventArgs e) 1111 { 1212 MessageBox.Show(((TextBox)sender).Text); 1313 }
进度条
<ProgressBar>样式 progress-bar
效果

代码
11 <ProgressBar Value="20" Style="{DynamicResource progress-bar}"></ProgressBar> 22 <ProgressBar Value="40" Style="{DynamicResource progress-bar-success}"></ProgressBar> 33 <ProgressBar Value="60" Style="{DynamicResource progress-bar-info}"></ProgressBar> 44 <ProgressBar Value="80" Style="{DynamicResource progress-bar-warning}"></ProgressBar> 55 <ProgressBar Value="100" Style="{DynamicResource progress-bar-danger}"></ProgressBar>
面板
<ContentControl>样式 panel
基本实例
效果

代码
11 <ContentControl Style="{StaticResource panel-default}"> 22 <ContentControl Style="{StaticResource panel-body}" Content="内容 Padding=15"/> 33 </ContentControl>
带标题的面版
效果

代码
11 <ContentControl Style="{StaticResource panel-default}"> 22 <StackPanel> 33 <ContentControl Style="{StaticResource panel-heading-default}" Content="标题 Padding=15,10"/> 44 <ContentControl Style="{StaticResource panel-body}" Content="内容"/> 55 </StackPanel> 66 </ContentControl>
带脚注的面版
效果

代码
11 <ContentControl Style="{StaticResource panel-default}"> 22 <StackPanel> 33 <ContentControl Style="{StaticResource panel-body}" Content="内容"/> 44 <ContentControl Style="{StaticResource panel-footer-default}" Content="脚标 Padding=15,10"/> 55 </StackPanel> 66 </ContentControl>
情境效果
效果

代码
1 1 <ContentControl Style="{StaticResource panel-primary}"> 2 2 <StackPanel> 3 3 <ContentControl Style="{StaticResource panel-heading-primary}" Content="primary"/> 4 4 <ContentControl Style="{StaticResource panel-body}"> 5 5 <ContentControl.Content> 6 6 <Grid> 7 7 <Grid.ColumnDefinitions> 8 8 <ColumnDefinition Width="auto"></ColumnDefinition> 9 9 <ColumnDefinition></ColumnDefinition> 1010 </Grid.ColumnDefinitions> 1111 <Grid.RowDefinitions> 1212 <RowDefinition Height="40"></RowDefinition> 1313 <RowDefinition Height="40"></RowDefinition> 1414 </Grid.RowDefinitions> 1515 <Label Grid.Column="0" Grid.Row="0" Content="内容:" VerticalAlignment="Center"></Label> 1616 <TextBox Grid.Column="1" Grid.Row="0" Margin="5,0,0,0" Text="BorderBrush=#428bca Foreground=#fff Background=#428bca"></TextBox> 1717 <Label Grid.Column="0" Grid.Row="1" Content="内容:" VerticalAlignment="Center"></Label> 1818 <TextBox Grid.Column="1" Grid.Row="1" Margin="5,0,0,0" Text="BorderBrush=#428bca Foreground=#fff Background=#428bca"></TextBox> 1919 </Grid> 2020 </ContentControl.Content> 2121 </ContentControl> 2222 </StackPanel> 2323 </ContentControl> 2424 <ContentControl Style="{StaticResource panel-success}"> 2525 <StackPanel> 2626 <ContentControl Style="{StaticResource panel-heading-success}" Content="success"/> 2727 <ContentControl Style="{StaticResource panel-body}" Content="BorderBrush=#d6e9c6 Foreground=#3c763d Background=#dff0d8"/> 2828 </StackPanel> 2929 </ContentControl> 3030 <ContentControl Style="{StaticResource panel-info}"> 3131 <StackPanel> 3232 <ContentControl Style="{StaticResource panel-heading-info}" Content="info"/> 3333 <ContentControl Style="{StaticResource panel-body}" Content="BorderBrush=#bce8f1 Foreground=#31708f Background=#d9edf7"/> 3434 </StackPanel> 3535 </ContentControl> 3636 <ContentControl Style="{StaticResource panel-warning}"> 3737 <StackPanel> 3838 <ContentControl Style="{StaticResource panel-heading-warning}" Content="warning"/> 3939 <ContentControl Style="{StaticResource panel-body}" Content="BorderBrush=#faebcc Foreground=#8a6d3b Background=#fcf8e3"/> 4040 </StackPanel> 4141 </ContentControl> 4242 <ContentControl Style="{StaticResource panel-danger}"> 4343 <StackPanel> 4444 <ContentControl Style="{StaticResource panel-heading-danger}" Content="danger"/> 4545 <ContentControl Style="{StaticResource panel-body}" Content="BorderBrush=#ebccd1 Foreground=#a94442 Background=#f2dede"/> 4646 </StackPanel> 4747 </ContentControl>
Glyphicons 字体图标
path资源,详情见http://www.cnblogs.com/tsliwei/p/6378659.html
效果

代码
1 <Path Style="{DynamicResource glyphicon}" Data="{DynamicResource glyphicon-asterisk}"></Path>
日期选择
<DatePicker>样式 datepicker 参考textbox
效果

代码
11 <DatePicker/> 22 <DatePicker IsEnabled="False"/> 33 <DatePicker Style="{StaticResource datepicker has-success}"/> 44 <DatePicker Style="{StaticResource datepicker has-warning}"/> 55 <DatePicker Style="{StaticResource datepicker has-error}"/>
2016-12-17更新:
感谢博友 散客游 的反馈,修复了进度条模糊的问题.
2016-12-19更新:
发布到GitHub,地址:https://github.com/ptddqr/bootstrap-wpf-style
2017-02-08更新:
添加Glyphicons字体图标
2018-04-03更新:
添加日期控件样式