C#/ WPF:ListView没有更新(但是当我用Snoop检查时,一切看起来都很好)

时间:2009-08-04 15:08:21

标签: wpf listview refresh snoop

有谁知道为什么我的ListView与以下代码不起作用? 我用Snoop检查了它,并且ItemsSource看起来很好(当我启动Snoop时,ListView会显示MyViewModel.MyCollection,但是在使用Visual Studio进行调试时它没有显示任何内容吗?)

谢谢!

PS:MainWindow.xaml.cs具有DataContext = MainViewModel

    <ListView Grid.Row="1" Margin="38,50,0,168" HorizontalAlignment="Left" Name="listViewSelectDate" Width="105"
              ItemsSource="{Binding Path=MyViewModel.MyCollection}" 
              SelectedItem="{Binding SelectedDate}" IsSynchronizedWithCurrentItem="True">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Date" DisplayMemberBinding="{Binding Path=CalcDate}"/>
            </GridView>
        </ListView.View>
    </ListView>

ViewModel如下所示:

class MainViewModel : ViewModelBase
{
    public SummaryViewModel MyViewModel
    {
        get { return _myViewModel; }
        set { _myViewModel = value; RaisePropertyChanged("MyViewModel"); }
    }
    public MyDate SelectedDate
    {
        get { return _selectedDate; }
        set { _selectedDate = value; RaisePropertyChanged("SelectedDate"); }
    }
}

public class SummaryViewModel : ViewModelBase
{
    public ObservableCollection<MyDate> MyCollection { get; set; }
}

public class MyDate
{
    public DateTime CalcDate { get; set; }
}

2 个答案:

答案 0 :(得分:1)

谁设置了MyCollection?它不提供更改通知,因此绑定不知道它已被更改。改为:

private ObservableCollection<MyDate> _myCollection;
public ObservableCollection<MyDate> MyCollection
{
    get { return _myCollection; }
    set
    {
        _myCollection = value;
        OnPropertyChanged("MyCollection");
    }
}

或者,更好的是,将其设为只读:

private readonly ICollection<MyDate> _myCollection = new ObservableCollection<MyDate>();

public ICollection<MyDate> MyCollection
{
    get { return _myCollection; }
}

答案 1 :(得分:0)

在Visual Studio输出窗口中查看,它将显示您可能获得的任何DataBinding错误,这可能有助于您解决问题。