WPF组合框选择索引绑定

时间:2018-07-01 21:50:29

标签: wpf combobox binding selectedindex

我很难将组合框的selectedindex绑定到对象。 这是我的代码:


  1. CustomerClass(的一部分)

    public class Customer : INotifyPropertyChanged
    {
    public int CountryCode
    {
        get { return _CountryCode; }
        set { _CountryCode = value; NotifyPropertyChanged(); }
    }
    }
    

2a。 (属于)CustomListItem

 <ComboBox x:Name="cboCountryCode" Grid.Column="5" ItemsSource="{Binding}" DisplayMemberPath="LongName" SelectedIndex="{Binding CountryCode, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

2b。 (属于)CustomListItem

public partial class CustomerListItem : UserControl
        {
            public CustomerListItem()
            {
                InitializeComponent();
                ObservableCollection<CountryCode> Liste = CountryCodes.Instance.List;
                cboCountryCode.DataContext = Liste;
            }
  1. (属于主页)

    <ItemsControl Name="itcCustomers" Style="{StaticResource ItemsControlVirtualizedStyle}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <local:CustomerListItem/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    

组合框列表项正确显示。
但是所选索引根本不起作用 See this screenshot

1 个答案:

答案 0 :(得分:1)

我发现了问题。我试图将组合框绑定到两个不同的数据源。一种用于集合,另一种用于selectedindex。现在,我将这两个数据源组合到一个类中并绑定到它上,现在它可以正常工作了

相关问题