我正在使用Silverlight 4,我很难让我的组合框正常工作。 在更改所选项时,selectedItem值保持为null。我将组合框定义如下:
<ComboBox
x:Name="ProductGroupCombobox"
Grid.Row="2"
Margin="10,15"
Height="30" Width="200"
Background="{x:Null}"
BorderBrush="{x:Null}"
ItemsSource="{Binding}"
SelectionChanged="ProductGroupCombobox_SelectionChanged"
SelectedItem="{Binding Path=ProductType, Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}" />
</DataTemplate>
<ComboBox.ItemTemplate>
</ComboBox>
有人有想法吗?
答案 0 :(得分:0)
您的SelectedItem
属性需要绑定到集合中的某个实例,而您似乎已将DataContext
设置为我假设的集合。请注意我是如何将绑定调整为集合的绑定以及将单独的属性调整为集合中实例的绑定。
public class MyData : INotifyPropertyChanged
{
List<String> ProductTypes {get; set;}
String _selectedProductType = String.Empty;
String SelectedProductType
{
get
{
return _selectedProductType;
}
set
{
_selectedProductType = value;
PropertyChangedEventHandler handler = PropertyChanged;
if(handler != null)
handler(this, new PropertyChangedEventArgs("SelectedProductType");
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
...
this.DataContext = new MyData();
...
<ComboBox
x:Name="ProductGroupCombobox"
Grid.Row="2"
Margin="10,15"
Height="30" Width="200"
Background="{x:Null}"
BorderBrush="{x:Null}"
ItemsSource="{Binding ProductTypes}"
SelectionChanged="ProductGroupCombobox_SelectionChanged"
SelectedItem="{Binding Path=SelectedProductType, Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>