使用模板建立RadioButton-List控件

 

前言:本文经过一个简单控件的建立,来看看silverlight的模板系统到底有多么强大(固然这只是强大之处的一点点点....)写做本文的灵感来自于我在互联网上闲逛,看到有朋友须要这样的需求,同时也想经过此练习来学习silverlight,但最但愿的是本文能对有须要的朋友有所帮助。html

 

 


 

 

 

控件需求:使用单选按钮来显示条目和接受选择的列表框vim

大概样子以下:ide

 

方案一:经过silverlight的元素合成思想学习

这个控件能够由如下两元素合成:spa

1:RadioButtoncode

2:ListBoxhtm

因而,最简单的代码就出来了:blog

 

< ListBox   Width ="150"  Height ="60" >
            
< RadioButton  Content ="单选按钮一" />
            
< RadioButton  Content ="单选按钮二" />
            
< RadioButton  Content ="单选按钮三" />
            
< RadioButton  Content ="单选按钮四" />
        
</ ListBox >

 

看看效果吧:get

 

 

看起来还不错~~那若是,咱们的需求有变化,须要默认选择第一个单选按钮,那继续改改吧:it

 

 

代码
< ListBox   Width ="150"  Height ="60" >
            
< ListBoxItem  IsSelected ="True" >
            
< RadioButton  Content ="单选按钮一" />
            
</ ListBoxItem >
            
< RadioButton  Content ="单选按钮二" />
            
< RadioButton  Content ="单选按钮三" />
            
< RadioButton  Content ="单选按钮四" />
        
</ ListBox >

 

 

结果以下:

 列表项第一个是被选中了,可是单选按钮仍旧没有被选中,那咱们就继续改改:

 

 

 

代码
< ListBox   Width ="150"  Height ="60" >
            
< ListBoxItem >
            
< RadioButton  Content ="单选按钮一"  IsChecked ="True" />
            
</ ListBoxItem >
            
< RadioButton  Content ="单选按钮二" />
            
< RadioButton  Content ="单选按钮三" />
            
< RadioButton  Content ="单选按钮四" />
        
</ ListBox >

 

把第一个按钮默认是选中的,看下结果:

 默认选中是能够了,可是,当咱们点击第二个按钮的时候,发现第一个按钮仍是选中的,这个问题好解决,把单选按钮归为一组就好了:

 

  < ListBox   Width ="150"  Height ="60" >
            
< ListBoxItem  >
            
< RadioButton  Content ="单选按钮一"  IsChecked ="True"  GroupName ="go" />
            
</ ListBoxItem >
            
< RadioButton  Content ="单选按钮二"  GroupName ="go" />
            
< RadioButton  Content ="单选按钮三"  GroupName ="go" />
            
< RadioButton  Content ="单选按钮四"  GroupName ="go" />
        
</ ListBox >

 

 

这样,一个目标需求的控件也算是合成了,可是其实它算不上正真意义上的控件,当你点击列表项的时候,单选按钮是不会被选中的,除非你点击列表项中的单选按钮,这样才能选中。其实也挺强大了,这里是把RadionButton做为ListBox的内容控件。那么,接下来,让咱们再来看看运用模板技术来更好的实现这个目标需求。

 

方案二:利用模板技术和绑定技术来实现

须要用到ListBox的ItemContainerStyle属性:

获取或设置在呈现项容器时使用的样式。

 

C#
public Style ItemContainerStyle { get; set; }

XAML 属性元素用法
<ListBox>
  <ListBox.ItemContainerStyle>
    inlineStyle
  </ListBox.ItemContainerStyle>
</ListBox>

而后制做ListBoxItem的模板,把它的模板内容设定为RadionButton,而后把RadioButton的IsChecked属性绑定到ListBoxItem的IsSelected属性上:

 

代码
< ListBox  ScrollViewer.VerticalScrollBarVisibility ="Visible"  Width ="100"  Height ="60"  x:Name ="lb" >
             
            
< ListBox.ItemContainerStyle >
                
< Style   TargetType ="ListBoxItem" >
                    
< Setter  Property ="Template" >
                        
< Setter.Value >
                            
< ControlTemplate  TargetType ="ListBoxItem" >
                                
< RadioButton  IsChecked =" {Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent}} "
                                             Content
=" {TemplateBinding Property=Content} " />
                            
</ ControlTemplate >
                        
</ Setter.Value >
                    
</ Setter >
                
</ Style >
            
</ ListBox.ItemContainerStyle >
            
< ListBoxItem   IsSelected ="True" >
                
< TextBlock > 单选按钮一 </ TextBlock >
            
</ ListBoxItem >    
                
< TextBlock > 单选按钮二 </ TextBlock >    
                
< TextBlock > 单选按钮三 </ TextBlock >
                
< TextBlock > 单选按钮四 </ TextBlock >
        
</ ListBox >

 

 

这样,当你选择列表项的时候,按钮的选中属性就会随着列表项的改变而改变,而且单选按钮也不须要分组了,由于这里它不是列表控件的内容控件了。看下最终效果:

              

相关文章
相关标签/搜索