1

我有一个列表<版本>,其中版本等具有属性版本UUID、标签、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的使用也不是一成不变的,如果有更好的解决方案来实现这一点所以只有一个项目被选中,我都愿意接受

感谢任何指针安德烈亚斯

4

1 回答 1

0
<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
}

我希望这将有所帮助。

于 2013-02-10T06:06:36.490 回答