wpf中容器设置线性渐变的背景以及foreach的⽤法
xaml代码:
<Window x:Class="WPF_Lab08.CheckBoxAndRadioButtonApp"
xmlns=""
xmlns:x=""
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 = "需要开发票!";
}
else if (rdbFalse.IsChecked == true)
{
string ss = "不需要开发票!";
}
else
{
string ss = "未知是否需要开发票!";
}
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=""
xmlns:x=""
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
⽀持丰富展现⼿段且界⾯统⼀的应⽤程序
checkbox和radiobutton的区别</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>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论