【问题标题】:Combobox doesn't display selected item组合框不显示所选项目
【发布时间】:2012-11-26 21:52:45
【问题描述】:

在代码中:

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

//State是一个有很多属性的类,其中一个是int 'Index'和int 'OneArrow'

public MainWindow()
{
...

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

this.DataContext = MyState;
}

在xml中:

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

绑定工作正常,没问题,但我还有另一个问题。 组合框不显示所选项目。我的意思是在下拉列表中突出显示正确的项目,但是当隐藏下拉列表时,什么都不会显示。

【问题讨论】:

  • 你能粘贴你的 State 类吗,是 State 的“Index”部分,并且是“OneArrow”,与“Index”的数据类型相同
  • public int OneArrow { get { return (int)GetValue(OneArrowProperty); } set { SetValue(OneArrowProperty, value); } } 公共 int 索引 { 获取 { 返回索引; } 设置 { 索引 = 值; this.IndexTextBlock.Text = "q" + value.ToString(); } }
  • OneArrow 和 Index 都是 int
  • 您是否将 ItemsSource 设置为 State 集合并尝试选择一个 int?这对我没有任何意义

标签: c# wpf combobox


【解决方案1】:

如果没有看到你所有的课程,很难猜出哪里出了问题。 这是我对您的代码的解释。

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>

代码背后:

 /// <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));
            }
        }
    }

这似乎工作正常,但就像我说的,我不知道你的 State 类是什么样子或 OneArrow 位于哪里(主窗体或状态)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多