简体   繁体   English

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

[英]C#/WPF: ListView not updating (but when I check with Snoop, everything looks fine)

Does anyone know why my ListView with following Code is not working? 有谁知道为什么我的带有以下代码的ListView无法正常工作? I checked it out with Snoop and the ItemsSource seems to be fine (and when I start Snoop, the ListView displays me the MyViewModel.MyCollection, but when debugging with Visual Studio it shows me nothing?) 我用Snoop进行了检查,ItemsSource看起来还不错(当我启动Snoop时,ListView向我显示MyViewModel.MyCollection,但是在使用Visual Studio进行调试时却什么也没显示?)

Thank you! 谢谢!

PS: MainWindow.xaml.cs has the DataContext = MainViewModel 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>

The ViewModel looks like this: 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"); }
    }
}

and

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

and

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

Who sets MyCollection ? 谁设置MyCollection It is not providing change notification, so the binding doesn't know that it has been changed. 它不提供更改通知,因此绑定不知道它已更改。 Change to: 改成:

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

Or, even better, make it read-only: 或者,甚至更好,将其设置为只读:

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM