【WPF】MVVM前台绑定一组RadioButton按钮
2017-01-18 12:08
1261 查看
需求:制作一组RadioButton,像下面这样的效果:
【MVVM】要显示一组RadioButton按钮,想法是Controller层联网获取到数据后,将数据进行处理,然后加到一个ObservableCollection集合中(或者List集合),然后前台准备一个列表控件,绑定这个集合。
最初想法是前台使用一个ItemsControl控件,然后设置它的DataTemplate,大致如下:
然后,运行后看不到这组按钮,但可以点击。去掉Style属性就只能显示RadioButton左边的圆圈,右边文字Content内容为空,而且按钮组无法切换,问题很多。。。。。
搜了一下解决方案,看到下面这个:
http://stackoverflow.com/questions/5891924/wpf-mvvm-radio-buttons-on-itemscontrol
根据老外的建议,应该使用ListBox而不是ItemsControl ,并且还得设置各种依赖属性的数据源,最后改为如下:
【MVVM】要显示一组RadioButton按钮,想法是Controller层联网获取到数据后,将数据进行处理,然后加到一个ObservableCollection集合中(或者List集合),然后前台准备一个列表控件,绑定这个集合。
最初想法是前台使用一个ItemsControl控件,然后设置它的DataTemplate,大致如下:
<ItemsControl x:Name="areaIC" ItemsSource="{Binding AreaNumList}"> <ItemsControl.ItemTemplate> <DataTemplate> <RadioButton Content="{Binding areaVal}" GroupName="area" Style="{StaticResource myRadioButton2}"/> </DataTemplate> </ItemsControl.ItemTemplate> </<ItemsControl>
然后,运行后看不到这组按钮,但可以点击。去掉Style属性就只能显示RadioButton左边的圆圈,右边文字Content内容为空,而且按钮组无法切换,问题很多。。。。。
搜了一下解决方案,看到下面这个:
http://stackoverflow.com/questions/5891924/wpf-mvvm-radio-buttons-on-itemscontrol
根据老外的建议,应该使用ListBox而不是ItemsControl ,并且还得设置各种依赖属性的数据源,最后改为如下:
<ListBox x:Name="areaLB" ItemsSource="{Binding AreaNumList}" SelectedItem="{Binding SelectedItem}" BorderThickness="0" Background="White"> <ListBox.ItemTemplate> <DataTemplate> <RadioButton x:Name="radioBtn" FontSize="14" GroupName="area" Style="{StaticResource myRadioButton2}"> <RadioButton.IsChecked> <Binding Path="IsSelected" RelativeSource="{RelativeSource AncestorType=ListBoxItem}" Mode="TwoWay" /> </RadioButton.IsChecked> <RadioButton.Content> <Binding Path="Content" RelativeSource="{RelativeSource AncestorType=ListBoxItem}" Mode="TwoWay" /> </RadioButton.Content> </RadioButton> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
相关文章推荐
- wpf 在MVVM模式下开发时,怎样让一个属性绑定到多个RadioButton上
- WPF MVVM 绑定RadioButton数据
- MVVM wpf listitem中绑定list外的命令的方法。
- Mvvm Light Toolkit for wpf/silverlight系列之数据绑定
- 【WPF】如何把一个枚举属性绑定到多个RadioButton
- WPF MVVM绑定DateGrid的列的显隐性解决方案
- wpf datagrid绑定数据中每行都有删除按钮
- WPF 如何在后台进行控件属性的绑定MVVM
- MVVM模式中WPF数据的完全绑定
- [WPF]MVVM模式下如何在后台cs中调用绑定命令
- WPF Prism框架下基于MVVM模式的命令、绑定、事件
- WPF中RadioButton绑定数据的正确方法
- WPF-MVVM双向绑定
- MVVM模式下WPF动态绑定展示图片
- 【WPF开发备忘】使用MVVM模式开发中列表控件内的按钮事件无法触发解决方法
- MVVM模式解析和在WPF中的实现(三)命令绑定
- WPF实现ComboBox绑定删除按钮
- ASP.NET中 RadioButtonList(单选按钮组),CheckBoxList(复选框组),DropDownList(下拉框),ListBox(列表框),BulletedList(无序列表)的使用前台绑定
- wpf mvvm datagrid DataGridTemplateColumn的绑定无效的可能原因之一!
- WPF中MVVM模式下控件自有的事件绑定