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

发布于 2024-07-30 13:22:08 字数 1450 浏览 9 评论 0原文

有谁知道为什么我的 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>

如下所示:

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; }
}

ViewModel

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

Does anyone know why my ListView with following Code is not working?
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?)

Thank you!

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

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; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

作业与我同在 2024-08-06 13:22:08

谁设置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; }
}

Who sets 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; }
}
用心笑 2024-08-06 13:22:08

查看 Visual Studio 输出窗口,它将显示您可能遇到的任何数据绑定错误,这可能有助于您解决问题。

Look in the Visual Studio Output window, it will show any DataBinding errors that you may be getting which may help you resolve the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文