wpf中容器设置线性渐变的背景以及foreach的用法
xaml代码:
<Window x:Class="WPF_Lab08.CheckBoxAndRadioButtonApp"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Name="gri" Height="290" Width="480">
<CheckBox Content="鸡肉卷" Height="16" HorizontalAlignment="Left" Margin="37,78,0,0" Name="chbFajita" VerticalAlignment="Top" />
<CheckBox Content="辣翅" Height="22" HorizontalAlignment="Left" Margin="37,33,0,0" Name="chbhotwing" VerticalAlignment="Top" Width="86" />
<CheckBox Content="可乐" Height="16" HorizontalAlignment="Left" Margin="264,33,0,0" Name="chbcoke" VerticalAlignment="Top" />
<CheckBox Content="烤鸡腿堡" Height="16" HorizontalAlignment="Left" Margin="163,33,0,0" Name="chbBurger" VerticalAlignment="Top" />
<CheckBox Content="玉米棒" Height="16" HorizontalAlignment="Left" Margin="163,78,0,0" Name="chbcob" VerticalAlignment="Top" />
<RadioButton Content="是" Height="16" HorizontalAlignment="Left" Margin="163,114,0,0" Name="rdbTrue" VerticalAlignment="Top" />
<RadioButton Content="否" Height="16" HorizontalAlignment="Left" Margin="264,114,0,0" Name="rdbFalse" VerticalAlignment="Top" />
<Label Content="是否开发票:" Height="29" HorizontalAlignment="Left" Margin="37,109,0,0" Name="label1" VerticalAlignment="Top" />
<Border BorderThickness="1" BorderBrush="Gray" CornerRadius="8" Margin="162,161,128,27">
<TextBlock Name="txtContent" Width="90" Height="90" Margin="202,173,211,48" FontSize="18" FontFamily="Adobe Fan Heiti Std" Foreground="Red">sd sad</TextBlock>
</Border>
<Button Content="购买" Height="23" HorizontalAlignment="Left" Margin="35,161,0,0" Name="btnGo" VerticalAlignment="Top" Width="75" Click="btnGo_Click" />
<Grid.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Offset="1" Color="Yellow"></GradientStop>
<GradientStop Offset="0" Color="Red"></GradientStop>
<GradientStop Offset="0.5" Color="Green"></GradientStop>
</LinearGradientBrush>
</Grid.Background>
</Grid>
</Window>
.cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPF_Lab08
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class CheckBoxAndRadioButtonApp : Window
{
public CheckBoxAndRadioButtonApp()
{
InitializeComponent();
}
string str = "";
private void btnGo_Click(object sender, RoutedEventArgs e)
{
Grid g = this.Content as Grid;
UIElementCollection children = g.Children;
foreach (UIElement u in children)
{
if (u is CheckBox)
{
CheckBox cb = u as CheckBox;
if (cb.IsChecked == true)
{ str += cb.Content + "\n"; }
}
}
if (rdbTrue.IsChecked == true)
{
string ss = "需要开发票!";
this.txtContent.Text = str + ss; str = "";
}
else if (rdbFalse.IsChecked == true)
{
string ss = "不需要开发票!";
this.txtContent.Text = str + ss; str = "";
}
else
{
string ss = "未知是否需要开发票!";
this.txtContent.Text = str + ss; str = "";
}
MessageBox.Show(txtContent.Text);
}
}
}
(foreach的用法):
XAML:
<StackPanel>
<CheckBox Content="a" IsChecked="True"/>
<CheckBox Content="b"/>
<CheckBox Content="c" IsChecked="True"/>
<CheckBox Content="d"/>
<CheckBox Content="e" IsChecked="True"/>
<CheckBox Content="f"/>
<Button Content="Show" Width="60" Click="Button_Click"></Button>
</StackPanel>
CS代码:
private void Button_Click(object sender, RoutedEventArgs e)
{
StackPanel sp = this.Content as StackPanel;
UIElementCollection children = sp.Children;
foreach (UIElement ui in children)
{
if (ui is CheckBox)
{
CheckBox cb = ui as CheckBox;
if (cb.IsChecked == true)
{
MessageBox.Show(cb.Content.ToString());
}
}
}
}
问题主要是涉及到继承的关系:
内容控件:主要用于包含一个子控件元素UIElement(UIElement是所有可视UI控件的基类,我叫它第三代控件祖宗)
容器控件:它可以包含有多个子控件元素(UIElement)
所以应该从UIElement入手,把容器里的子控件UIElement遍历出来,也即成了一个集合,便 是:UIElementCollection了。。
ScrollViewer用法:
<Window x:Class="ContainerDemo.ScrollViewerDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ScrollViewerDemo" Height="300" Width="300">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<FlowDocument PageHeight="900">
<Paragraph>
Microsoft.NET 3.0的构成主要包括.NET Framework2.0
和新增加的3个模块,具体包括:Windows Workflow Foundation
支持基于工作流的应用程序,Windows Communication Foundation
支持面向服务的应用程序,Windows Presentation Foundation
支持丰富展现手段且界面统一的应用程序
</Paragraph>
<Paragraph>
Microsoft.NET 3.0的构成主要包括.NET Framework2.0
和新增加的3个模块,具体包括:Windows Workflow Foundation
支持基于工作流的应用程序,Windows Communication Foundation
支持面向服务的应用程序,Windows Presentation Foundation
支持丰富展现手段且界面统一的应用程序
</Paragraph>
<Paragraph>
Microsoft.NET 3.0的构成主要包括.NET Framework2.0
和新增加的3个模块,具体包括:Windows Workflow Foundation
支持基于工作流的应用程序,Windows Communication Foundation
支持面向服务的应用程序,Windows Presentation Foundation
支持丰富展现手段且界面统一的应用程序
</Paragraph>
<Paragraph>
Microsoft.NET 3.0的构成主要包括.NET Framework2.0
和新增加的3个模块,具体包括:Windows Workflow Foundation
支持基于工作流的应用程序,Windows Communication Foundation
支持面向服务的应用程序,Windows Presentation Foundation
支持丰富展现手段且界面统一的应用程序
</Paragraph>
</FlowDocument>
</ScrollViewer>
</Window>

浙公网安备 33010602011771号