Fork me on GitHub

CheckBox

 

        CheckBox也和通常的WPF控件一样,可以定义样式和事件,同时CheckBox中可以嵌套其他控件!例子如下:

<Page

       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

       xmlns:d="http://schemas.microsoft.com/expression/blend/2006"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"

       x:Class="Test.checkboxtest"

       x:Name="Page"

       WindowTitle="Page"

       FlowDirection="LeftToRight"

       Width="640" Height="480"

       WindowWidth="640" WindowHeight="480" AllowDrop="False" IsEnabled="False">

<Page.Resources>

       <Style x:Key="Triggers" TargetType="{x:Type CheckBox}">

<Style.Triggers>

    <Trigger Property="CheckBox.IsPressed" Value="true">

       <Setter Property = "Foreground" Value="Green"/>

    </Trigger>

    <Trigger Property="CheckBox.IsMouseOver" Value="true">

       <Setter Property = "Background" Value="Red"/>

          <Setter Property = "IsChecked" Value="true"/>

    </Trigger>

</Style.Triggers>

</Style>

</Page.Resources>

<Grid x:Name="LayoutRoot">

<CheckBox Name="CheckBox1" HorizontalAlignment="Left" Margin="34,63,0,0"

       VerticalAlignment="Top" Width="211" Height="56" Checked="HandleChange" Style="{StaticResource Triggers }">

       <Image Source="images\apple.jpg" Height="43" Width="43" d:LayoutOverrides="Width, Height" HorizontalAlignment="Left" Margin="62,16,0,0" VerticalAlignment="Top"/>

</CheckBox>

</Grid>

</Page>

CS下的代码如下:

        private void HandleChange(object sender, RoutedEventArgs e)

        {

            CheckBox1.Content = "Change Text";

        }
本文参考MSDN组织。

posted @ 2007-03-14 23:31  桂素伟  阅读(611)  评论(0编辑  收藏  举报