深入浅出WPF 第二部分(28)

本文介绍了一个使用WPF技术实现的学生信息管理系统,详细解释了如何利用WPF创建用户界面,展示学生数据,并通过样式和触发器进行个性化设置。文章还探讨了如何在系统中查找特定学生信息,以及如何利用Visual Tree Helper类来查找控件。
<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:objs="clr-namespace:WpfApp2.Objects"
        xmlns:col="clr-namespace:System.Collections;assembly=mscorlib"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="300" Width="300">
    <Window.Resources>
        <col:ArrayList x:Key="ds">
            <objs:Student Name="Timothy" Skill="WPF" HasJob="True" Id="1"/>
            <objs:Student Name="Anders" Skill="C#" HasJob="True" Id="2"/>
            <objs:Student Name="Tommy" Skill="ASP.NET" HasJob="True" Id="3"/>
            <objs:Student Name="Jim" Skill="WCF" HasJob="True" Id="4"/>
            <objs:Student Name="Andy" Skill="WF" HasJob="False" Id="5"/>
            <objs:Student Name="Jack" Skill="JAVA" HasJob="True" Id="6"/>
        </col:ArrayList>
        
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Width" Value="60"/>
        </Style>

        <DataTemplate x:Key="nameDT" DataType="{x:Type objs:Student}">
            <TextBox x:Name="textBoxName" Width="60" Text="{Binding Name}" GotFocus="textBoxName_GotFocus"/>
        </DataTemplate>

        <DataTemplate x:Key="skillDT">
            <TextBox x:Name="textBoxSkill" Width="60" Text="{Binding Skill}"/>
        </DataTemplate>

        <DataTemplate x:Key="hasJobDT">
            <CheckBox Width="60" IsChecked="{Binding HasJob}"/>
        </DataTemplate>
    </Window.Resources>
    
    <StackPanel>
        <ListView x:Name="lv" ItemsSource="{StaticResource ds}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="ID" DisplayMemberBinding="{Binding Id}"/>
                    <GridViewColumn Header="姓名" CellTemplate="{StaticResource nameDT}"/>
                    <GridViewColumn Header="技术" CellTemplate="{StaticResource skillDT}"/>
                    <GridViewColumn Header="已工作" CellTemplate="{StaticResource hasJobDT}"/>
                </GridView>
            </ListView.View>
        </ListView>
        <Button Content="Find" Margin="5,5" Click="Button_Click" Width="80"/>
    </StackPanel>
</Window>

        private void textBoxName_GotFocus(object sender, RoutedEventArgs e)
        {
            TextBox tb = e.OriginalSource as TextBox;
            ContentPresenter cp = tb.TemplatedParent as ContentPresenter;

            Student stu = cp.Content as Student;
            this.lv.SelectedItem = stu;

            ListViewItem lvi = this.lv.ItemContainerGenerator.ContainerFromItem(stu) as ListViewItem;
            CheckBox cb = FindVisualChild<CheckBox>(lvi);
            MessageBox.Show(cb.IsChecked.ToString());        
        }

        public ChildType FindVisualChild<ChildType>(DependencyObject obj)
            where ChildType:DependencyObject
        {
            //借助VisualTreeHelper类获取子控件
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);

                if (child != null && child is ChildType)
                    return child as ChildType;
                else
                {
                    ChildType childOfChild = FindVisualChild<ChildType>(child);
                    if (childOfChild != null)
                        return childOfChild;
                }
            }
            return null;
        }

11.5 深入浅出话Style

构成Style最重要的两种元素是Setter和Trigger,Setter类帮助我们设置控件的静态外观风格,Trigger类则帮助我们设置控件的行为风格。

11.5.1 Style中的Setter

<Window x:Class="WpfApp3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="FontSize" Value="24"/>
            <Setter Property="TextDecorations" Value="Underline"/>
            <Setter Property="FontStyle" Value="Italic"/>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <TextBlock Text="Hello WPF!"/>
            <TextBlock Text="This is a sample Style!"/>
            <TextBlock Text="by Anders 1/5/13" Style="{x:Null}"/>
        </StackPanel>
    </Grid>
</Window>

11.5.2 Style中的Trigger

1. 基本Trigger

<Window x:Class="WpfApp3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="{x:Type CheckBox}">
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="FontSize" Value="24"/>
                    <Setter Property="Foreground" Value="Orange"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <CheckBox Content="月光宝盒" IsChecked="True"/>
            <CheckBox Content="大话西游" IsChecked="False"/>
        </StackPanel>
    </Grid>
</Window>

2. MultiTrigger

MultiTrigger实际上叫MultiConditionTrigger更合适,因为必须多个条件同时成立时才会被触发。

<Window x:Class="WpfApp3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="{x:Type CheckBox}">
            <Style.Triggers>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsChecked" Value="True"/>
                        <Condition Property="Content" Value="大话西游"/>
                    </MultiTrigger.Conditions>
                    <Setter Property="FontSize" Value="24"/>
                    <Setter Property="Foreground" Value="Orange"/>
                </MultiTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <CheckBox Content="月光宝盒" IsChecked="True"/>
            <CheckBox Content="大话西游" IsChecked="True"/>
            <CheckBox Content="大话西游" IsChecked="False"/>
            <CheckBox Content="月光宝盒" IsChecked="True"/>
        </StackPanel>
    </Grid>
</Window>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值