【问题标题】:WPF Combobox, bind a collection<items> where item has a bool property isSelected?WPF Combobox,绑定一个集合<items>,其中项目具有布尔属性isSelected?
【发布时间】:2013-02-10 03:32:24
【问题描述】:

我有一个列表,其中Version 具有VersionUUID、Label、SKU 和IsSelected 属性。我想将它绑定到一个 Combobox 并让所选项目只选择 IsSelected 标志(未选择任何先前设置的标志)。

注意:组合框在模板中,在数据网格单元格中使用,所以我不能只将 SelectedItem 绑定到模型!

到目前为止,我正在工作,数据网格按预期更新数据库,但初始值未设置 onLoad。如果一个版本已经有 IsSelected=true,我希望在 Combobox 中显示它,但它始终为空,除非我从列表中选择一个。

    <DataTemplate x:Key="dtDatagridVersionSelector">
        <ComboBox Margin="0" Width="90"  Style="{StaticResource DatagridComboBox}"
                  ItemsSource="{Binding Path=Versions, Mode=OneTime}">
            <ComboBox.ItemTemplate >
                <DataTemplate >
                    <RadioButton Focusable="false"  IsEnabled="true" 
                                  GroupName="{Binding VersionUUID}" 
                                  IsChecked="{Binding IsSelected, Mode=TwoWay}">
                        <StackPanel Orientation="Horizontal" >
                            <TextBlock Margin="3,0,0,0"  Text="{Binding Label}"/>
                            <TextBlock Foreground="Red" Margin="3,0,0,0" 
                                       Text="{Binding SKU}"/>
                        </StackPanel>
                    </RadioButton>
                </DataTemplate>
            </ComboBox.ItemTemplate>
            <ComboBox.ItemContainerStyle>
                <Style TargetType="ComboBoxItem">
                    <Setter Property="IsSelected" 
                        Value="{Binding IsSelected, Mode=OneWay}" />
                </Style>
            </ComboBox.ItemContainerStyle>
        </ComboBox>
    </DataTemplate>

另外,Radiobox 的使用也不是一成不变的,如果有更好的解决方案来实现这一点,所以只有一个项目是选中的,我都愿意接受它

感谢任何指针 安德烈亚斯

【问题讨论】:

    标签: wpf binding combobox selecteditem


    【解决方案1】:
    <Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:SelectedItemConverter x:Key="selectedItemConverter"/>
    </Window.Resources>
    <Grid>
        <ComboBox ItemsSource="{Binding Students}" SelectedItem="{Binding Students, Converter={StaticResource selectedItemConverter}}" DisplayMemberPath="Name"/>
    </Grid>
    

        public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Students = new ObservableCollection<Student>();
            Students.Add(new Student() { Name = "HArish", RollNo = 1, IsSelected = false });
            Students.Add(new Student() { Name = "Arev", RollNo = 2, IsSelected = false });
            Students.Add(new Student() { Name = "Pankaj", RollNo = 3, IsSelected = true });
            Students.Add(new Student() { Name = "Deepak", RollNo = 4, IsSelected = false });
            DataContext = this;
        }
        public ObservableCollection<Student> Students { get; set; }
    }
    public class Student
    {
        public string Name { get; set; }
        public int RollNo { get; set; }
        public bool IsSelected { get; set; }
    }
    
    public class SelectedItemConverter : IValueConverter
    {
    
        #region IValueConverter Members
    
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && value is IEnumerable<Student>)
                return ((IEnumerable<Student>)value).Where(s => s.IsSelected).FirstOrDefault();
            return null;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    
        #endregion
    }
    

    我希望这会有所帮助。

    【讨论】:

    • 感谢您的详细示例@ethicallogics。昨晚我以我能想到的任何方式多次实现了这个转换器,但它从未最终选择初始项目。今天早上编写了您的示例,实现了我的单选/单选,它按预期工作。谢谢
    猜你喜欢
    • 2010-09-23
    • 2018-03-18
    • 2014-11-24
    • 2010-09-17
    • 2012-01-27
    • 2016-05-09
    • 2021-10-02
    • 1970-01-01
    相关资源
    最近更新 更多