2010-05-17 101 views
1

我有一个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。

请帮忙!!

回答

1

之前,我没有得到这个部分:

我打电话MoveUp和MoveDown方法 Observable Collection。

ObservableCollection没有这样的方法,至少不是我所知?它也没有一个当前项目或类似的概念。

我很抱歉,如果我错过了一些可能使这篇文章无知的东西。

你可以做的不是将你的ListView绑定到一个ObservableCollection,而是你可以将它绑定到一个ICollectionView(派生自你的ObservableCollection)。如果在ListView上设置IsSynchronizedWithCurrentItem = True,则不需要绑定SelectedItem,它将自动绑定到ICollectionView上的CurrentItem。

ICollectionView还实现了MoveCurrentToNext和MoveCurrentCurrentToPrevious,它们可以从您的按钮(通过ICommand)进行绑定。

编辑: 现在,新的信息在桌子上,我的上面的答案是不是真的有关了。但是我还没有知道SO约定如何处理这个问题,如果我应该完全删除帖子,请修改上面的内容或者添加“新的”回复。现在我将编辑这篇文章。

我试图重新创建您的项目和您的问题。希望我已经理解了你的问题,并至少重新创建了它。

由于在组合框被移动到列表视图中时,组合框没有保存它的值,所以它适用于我。

下面是相关的代码(其中的一些隐藏,以避免太多的噪音)。

这是否帮助你吗?

public class MainWindowViewModel:INotifyPropertyChanged 
    { 
     private Condition _selectedCondition; 
     public ObservableCollection<Condition> Conditions { get; set; } 

     public Condition SelectedCondition 
     { 
      get 
      { 
       return _selectedCondition; 
      } 
      set 
      { 
       if (_selectedCondition != value) 
       { 
        _selectedCondition = value; 
        OnPropertyChanged("SelectedCondition"); 
       } 
      } 
     } 
... 
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); 
     } 
    } 

然后,条件类:

public class Condition : INotifyPropertyChanged 
    { 
     private MyType myType; 
     public ObservableCollection<MyType> MyTypes { get; set; } 

     public MyType MyType 
     { 
      get { return myType; } 
      set 
      { 
       if (myType != value) 
       { 
        myType = value; 
        OnPropertyChanged("MyType"); 

       } 
      } 
     } 


     public Condition() 
     { 
      MyTypes = new ObservableCollection<MyType>() { new MyType() { Key = "1", Value = "One" }, new MyType() { Key = "2", Value = "Two" } }; 
      MyType = MyTypes[1]; 
     } 

...等

<ListView 
      SelectedItem="{Binding Path=SelectedCondition}" 
      ItemsSource="{Binding Path=Conditions}"     
      FontWeight="Normal" 
      FontSize="11"         
      Name="listview" Margin="0,32,0,0"> 
      <ListView.View> 
       <GridView> 
        <GridViewColumn       
         Width="175" 
         Header="Type"> 
         <GridViewColumn.CellTemplate> 
          <DataTemplate> 
           <ComboBox          
            Width="150" 
            SelectedValuePath="Key" 
            DisplayMemberPath="Value"  
            SelectedItem="{Binding Path=MyType}" 
            ItemsSource="{Binding Path=MyTypes}" 
            HorizontalAlignment="Center" /> 
          </DataTemplate> 
         </GridViewColumn.CellTemplate> 
        </GridViewColumn> 
       </GridView> 
      </ListView.View> 
     </ListView> 
     <Button Content="Move up" 
       Command="{Binding MoveUpCommand}" 
       /> 
+0

感谢您的回复。对不起,ObservableCollection的名称不正确,ObservableCollection中的Move方法将集合中的实体从旧索引移动到新索引 – 2010-05-17 08:02:57

+0

啊我明白了。所以,为了清楚起见,你想移动它自己的对象,就像按照项目的变化顺序一样,不要按照我的想法移动选择项目? – ThomasAndersson 2010-05-17 08:16:50

+0

是的,我将ListView的selectedItem设置为当前移动的对象,它在列表视图中给出选择 – 2010-05-17 08:23:06

0

交换这些行:

SelectedItem="{Binding Path = SelectedCondition}" 
ItemsSource="{Binding Path = Conditions}" 

的ItemSource需要是的SelectedItem

+0

感谢您的答复丹尼尔忽略了最低于事无补.. – 2010-05-17 05:53:50

+0

您是否尝试过操纵的SelectedIndex在listview上,而不是在observableCollection上的Move方法,我从来没有真正使用过它们。 – Daniel 2010-05-17 05:57:21

+0

我试着设置选定的索引= 0(硬编码),但是这也不能帮助我... – 2010-05-17 05:58:41

0

你尝试ItemsSource="{Binding Conditions}"

+0

我刚刚尝试过,但它没有解决问题。我仍然无法保留列表视图组合框的选定索引。 – 2010-05-17 06:48:32

+0

一些代码将有所帮助。同时在执行MoveUp操作时QuickWatch MyType属性。 – Amsakanna 2010-05-17 07:02:51

+0

从您的代码中可以明显看出,MyType是模型,MyTypes是列表。但不知道为什么你将选定的项目绑定到MyType。不应该是SelectedMyType,一个必须创建的属性? – Amsakanna 2010-05-17 08:33:48