silverlight控件模型之选择控件和列表控件

很高兴能再次和大家分享,多的不说了,继续开始.......

选择控件:checkbox控件和radiobutton控件

 <StackPanel Background="White" VerticalAlignment="Center">
            <CheckBox Content="中间状态" IsThreeState="True" IsChecked="" Margin="20"></CheckBox>
            <CheckBox Content="选中" IsThreeState="True" IsChecked="true" Margin="20"></CheckBox>
            <CheckBox Content="未选中" IsThreeState="True" IsChecked="false" Margin="20"></CheckBox>
        </StackPanel>
        <StackPanel Background="White" VerticalAlignment="Center">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="请选择性别:"></TextBlock>
                <RadioButton Content="男" Margin="20" IsChecked="true"></RadioButton>
                <RadioButton Content="女" Margin="20" IsChecked="false"></RadioButton>
            </StackPanel>
            <StackPanel>
                <RadioButton Content="1" GroupName="g1" Margin="10">
                </RadioButton>
                <RadioButton Content="2" GroupName="g1" Margin="10"></RadioButton>
                <RadioButton Content="3" GroupName="g1" Margin="10"></RadioButton>
            </StackPanel>
        </StackPanel>

注意:checkbox控件有个很重要的属性isthreestate,它可以支持3中状态,选中、未选中、中间状态,比.NET的CHECKBOX强大吧 ,嘿嘿

在使用RADIOBUTTON要注意,如果RADIOBUTTON处于同一容器,就不需要设置GROUPNAME来识别.radiobutton控件属于内容控件,我们就可以自定义它的样式了,这里就不多讲了吧,前面一篇有例子.

 

 

slider控件:范围控件

 <StackPanel Background="White" VerticalAlignment="Center">
        <TextBlock Text="默认slider控件" Margin="10"></TextBlock>
            <Slider x:Name="sa" Margin="5" Maximum="100" Minimum="0" Value="20"></Slider>
            <TextBlock Text="垂直SILDER控件"></TextBlock>
            <Slider x:Name="sb" Margin="5" Maximum="100" Minimum="0" Orientation="Vertical" Height="180" IsDirectionReversed="False" ValueChanged="sb_ValueChanged"></Slider>
            <TextBlock x:Name="myvalue" Margin="20"></TextBlock>
        </StackPanel>

 private void sb_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            this.myvalue.Text = "当前值:"+this.sb.Value;
        }

属性介绍:maxinum:设置控件的最大值,minimun:设置控件数字范围的最小值,value是当前值,isdirctinoreversed:设置控件的增加方向,如果为FALSE,方向朝上就增加,反之..,orientation:设置控件的方向有垂直和水平

 

列表控件:combox控件和listbox控件

两个控件拥有共同的使用方法,我这里就只讲解combox的使用

 <StackPanel Background="White" VerticalAlignment="Center">
            <ComboBox Width="300">
                <ComboBoxItem>
                    <ComboBoxItem.Content>
                        <Rectangle Fill="Red" Width="200" Height="30"></Rectangle>
                    </ComboBoxItem.Content>
                </ComboBoxItem>
                <ComboBoxItem>
                    <ComboBoxItem.Content>
                        <TextBlock Text="你好"></TextBlock>
                    </ComboBoxItem.Content>
                </ComboBoxItem>
            </ComboBox>
        </StackPanel>

首先combox属于内容控件可以自定义,接下来就演示动态创建combox中的内容

定义实体类: public class Category
    {
        public int ID {get;set; }
        public string Name { get; set; }
        public int Count { get; set; }
    }

动态绑定到combox中

 public ListControl()
        {
            InitializeComponent();
            this.Loaded+=new RoutedEventHandler(ListControl_Loaded);
        }
        public void ListControl_Loaded(object sender, RoutedEventArgs e)
        {
            List<Category> list = new List<Category> {
                new Category{ID=1,Name="命令控件",Count=10},
                new Category{ID=2,Name="选择控件",Count=20},
                new Category{ID=3,Name="列表控件",Count=30},
            };
            this.morecombox.ItemsSource = list;
        }

 

前台:

<StackPanel Background="White" VerticalAlignment="Center">
            <ComboBox x:Name="mycombox" Width="200" Height="30" DisplayMemberPath="Name"></ComboBox>
        </StackPanel>
        <StackPanel Background="White" VerticalAlignment="Top">
            <ComboBox x:Name="morecombox" Width="200" Height="30" SelectionChanged="morecombox_SelectionChanged">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Width="260" Orientation="Horizontal">
                            <TextBlock Text="{Binding ID}"></TextBlock>
                            <TextBlock Text="."></TextBlock>
                            <TextBlock Text="{Binding Name}"></TextBlock>
                            <TextBlock Text=" ( "></TextBlock>
                            <TextBlock Text="{Binding Count}" Foreground="Red"></TextBlock>
                            <TextBlock Text=" ) "></TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
            <TextBlock x:Name="info"></TextBlock>
        </StackPanel>

属性介绍:Displaymemberpath获取和设置显示的名称,itemssource:数据原集合,itemtemplate:列的模板类似于.NET中GRIDVIEW中的模板列,如果还要获取选中的某一行selectindex或者selectitem属性,还有一个常用的就是selectchanged事件

   private void morecombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Category ca = this.morecombox.SelectedItem as Category;
            this.info.Text = ca.Name;
        }

好了,COMBOX常用就这么多了,它和LISTBOX使用都一样,特别注意就是LISTBOX还是内容控件但是在使用的时候就不用这样使用:<listbox.content>貌似没有吧,就直接使用<listboxitem>下面直接加就行了。。。。。。。

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/T-MAC1988/archive/2009/12/23/1630928.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值