我有一个ListView的ItemSource被设置为我的自定义集合。为什么ListView(WPF)中的数据绑定失败?
我已经定义了一个GridView CellTemplate包含如下组合框:
<ListView
MaxWidth="850"
Grid.Row="1"
SelectedItem="{Binding Path = SelectedCondition}"
ItemsSource="{Binding Path = Conditions}"
FontWeight="Normal"
FontSize="11"
Name="listview">
<ListView.View>
<GridView>
<GridViewColumn
Width="175"
Header="Type">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox
Style="{x:Null}"
x:Name="TypeCmbox"
Height="Auto"
Width="150"
SelectedValuePath="Key"
DisplayMemberPath="Value"
SelectedItem="{Binding Path = MyType}"
ItemsSource="{Binding Path = MyTypes}"
HorizontalAlignment="Center" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</ListView>
public MyType : INotifyPropertyChanged
{ 串钥匙; 字符串值;
public string Key { get { return _key; }
set { _key = value; this.OnPropertyChanged("Key"); } }
public string Value { get { return _value; }
set { _value = value; this.OnPropertyChanged("Value"); } }
public MyType()
{
}
public MyType (string key, string value)
{
_key = key;
_value = value;
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
public void MoveUpExecuted()
{
int oldIndex = this.Conditions.IndexOf(_selectedCondition);
//Check if the selected item is not the first item
if (oldIndex != 0)
{
int newIndex = oldIndex - 1;
this.Conditions.Move(oldIndex, newIndex);
}
}
我的自定义收藏是的ObservableCollection。
我有一个两个按钮 - 在列表视图控件的顶部上移和下移。当用户单击“上移”或“下移”按钮时,我将调用Observable Collection的Move方法。
但是当我上移和下移行时,组合框的选定索引是-1。
我已经确保在执行上移和下移命令时selectedItem不等于null。
请帮忙!!
感谢您的回复。对不起,ObservableCollection的名称不正确,ObservableCollection中的Move方法将集合中的实体从旧索引移动到新索引 – 2010-05-17 08:02:57
啊我明白了。所以,为了清楚起见,你想移动它自己的对象,就像按照项目的变化顺序一样,不要按照我的想法移动选择项目? – ThomasAndersson 2010-05-17 08:16:50
是的,我将ListView的selectedItem设置为当前移动的对象,它在列表视图中给出选择 – 2010-05-17 08:23:06