如果没有看到你所有的课程,很难猜测出了什么问题。这是我对您的代码外观的解释。
xml:
<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 位于哪里(mainform 或 State)