Combobox不显示所选项目

时间:2021-01-22 02:24:02

in code:

private ObservableCollection<State> allStates = new ObservableCollection<State>();

//State is a class with many properties, one of them is int 'Index' and int 'OneArrow'

// State是一个包含许多属性的类,其中一个是int'Index'和int'OneArrow'

public MainWindow()
{
...

this.MyComboBox.ItemsSource = allStates;
this.MyComboBox.DisplayMemberPath = "Index";
this.MyComboBox.SelectedValuePath = "Index";

this.DataContext = MyState;
}

in xaml:

<ComboBox Name="MyComboBox" 
Width="60" Height="20" 
IsEnabled="False" 
SelectedValue="{Binding Path=OneArrow, UpdateSourceTrigger=PropertyChanged}"/>

binding is working, it's fine, but I have another problem. Combobox doesn't dsiplay selected item. I mean in a drop-down list a right item is highlighted, but when drop-down list is hidden, nothing is diplayed.

绑定工作,没关系,但我有另一个问题。 Combobox不会选择项目。我的意思是在下拉列表中突出显示正确的项目,但是当隐藏下拉列表时,不会显示任何内容。

Combobox不显示所选项目

1 个解决方案

#1


0  

Without seeing all you classes its hard to guess whats going wrong. Here is my interprtation on what your code looks like.

没有看到你所有的课程很难猜测什么是错的。这是我对你的代码看起来像什么的插入。

Xaml:

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Name="UI">
    <Grid >
        <ComboBox ItemsSource="{Binding ElementName=UI, Path=AllStates}" 
                  DisplayMemberPath="Index"
                  SelectedValuePath="Index"
                  SelectedValue="{Binding ElementName=UI, Path=OneArrow}"
                  Height="21" HorizontalAlignment="Left" Margin="80,82,0,0" Name="comboBox1" VerticalAlignment="Top" Width="152" />
    </Grid>
</Window>

Code Behind:

 /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            AllStates.Add(new State { Index = 1 });
            AllStates.Add(new State { Index = 2 });
            AllStates.Add(new State { Index = 3 });
            AllStates.Add(new State { Index = 4 });
        }

        private ObservableCollection<State> allStates = new ObservableCollection<State>();
        public ObservableCollection<State> AllStates 
        {
            get { return allStates; }
            set { allStates = value; }
        }

        private int oneArrow;
        public int OneArrow
        {
            get { return oneArrow; }
            set { oneArrow = value; }
        }

    }

    public class State : INotifyPropertyChanged
    {
        private int index;
        public int Index
        {
            get { return index; }
            set { index = value; NotifyPropertyChanged("Index"); }
        }


        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
    }

This seems to work ok, but like I said I have no idea what your State class looks like or where OneArrow is located (mainform or State)

这似乎工作正常,但就像我说我不知道​​你的State类看起来是什么或OneArrow位于何处(mainform或State)

#1


0  

Without seeing all you classes its hard to guess whats going wrong. Here is my interprtation on what your code looks like.

没有看到你所有的课程很难猜测什么是错的。这是我对你的代码看起来像什么的插入。

Xaml:

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Name="UI">
    <Grid >
        <ComboBox ItemsSource="{Binding ElementName=UI, Path=AllStates}" 
                  DisplayMemberPath="Index"
                  SelectedValuePath="Index"
                  SelectedValue="{Binding ElementName=UI, Path=OneArrow}"
                  Height="21" HorizontalAlignment="Left" Margin="80,82,0,0" Name="comboBox1" VerticalAlignment="Top" Width="152" />
    </Grid>
</Window>

Code Behind:

 /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            AllStates.Add(new State { Index = 1 });
            AllStates.Add(new State { Index = 2 });
            AllStates.Add(new State { Index = 3 });
            AllStates.Add(new State { Index = 4 });
        }

        private ObservableCollection<State> allStates = new ObservableCollection<State>();
        public ObservableCollection<State> AllStates 
        {
            get { return allStates; }
            set { allStates = value; }
        }

        private int oneArrow;
        public int OneArrow
        {
            get { return oneArrow; }
            set { oneArrow = value; }
        }

    }

    public class State : INotifyPropertyChanged
    {
        private int index;
        public int Index
        {
            get { return index; }
            set { index = value; NotifyPropertyChanged("Index"); }
        }


        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
    }

This seems to work ok, but like I said I have no idea what your State class looks like or where OneArrow is located (mainform or State)

这似乎工作正常,但就像我说我不知道​​你的State类看起来是什么或OneArrow位于何处(mainform或State)