本文介绍了主要细节MVVM WPF不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让我的绑定在Detail ListView上工作。我已将所有的MVVM模式代码粘贴到下面。请帮助!!!

I am unable to get my bindings working on the Detail ListView. I have pasted all my MVVM pattern code below. Please help!!!

我的视图:
DirectoryDe​​tailView.cs

My View : DirectoryDetailView.cs

<UserControl x:Class="S2.Views.DirectoryDetailView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <ListView Grid.Column="0" ItemsSource="{Binding Path = DirectoryDetails}"
              IsSynchronizedWithCurrentItem="True"
              SelectedItem="{Binding SelectedDirName, Mode=TwoWay}">
        <ListView.View>
            <GridView>
                <GridViewColumn DisplayMemberBinding="{Binding Path = FileName}"
                                Header="File Name"/>
            </GridView>
        </ListView.View>
    </ListView>
    <ListView Grid.Column="1" Margin="10,0,0,0" ItemsSource="{Binding Path = DirectoryDetails}">
        <ListView.View>
            <GridView>
                <GridViewColumn DisplayMemberBinding="{Binding Path = FileDetails.Length}"
                                Header="Length"/>
                <GridViewColumn DisplayMemberBinding="{Binding Path = FileDetails.LastAccessTime}"
                                Header="LastAccessTime"/>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

我的模型:

public class DirectoryModel : INotifyPropertyChanged
{
    private string _fileName;
    private DateTime _createdTime;

    public string FileName
    {
        get
        {
            return _fileName;
        }
        set
        {
            _fileName = value;
            RaisePropertyChanged("FileName");
        }
    }

    private IEnumerable<FileDetails> _fileDetails;

    public IEnumerable<FileDetails> FileDetails
    {
        get
        {
            return _fileDetails;
        }
        set
        {
            _fileDetails = value;
            RaisePropertyChanged("FileDetails");
        }

    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    protected void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler propertyChanged = PropertyChanged;

        if (propertyChanged != null)
        {
            propertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

public class FileDetails
{
    public long Length { get; set; }

    public DateTime LastAccessTime { get; set; }
}

我的ViewModel:

My ViewModel:

public class DirectoryViewModel : BaseViewModel
{
    private IEnumerable<DirectoryModel> _directoryDetails;

    public IEnumerable<DirectoryModel> DirectoryDetails
    {
        get
        {
            var service = GetService<IDirectoryService>();
            _directoryDetails = service.GetDirectoryDetails();
            return _directoryDetails;
        }
        set
        {
            if(_directoryDetails != value)
            {
                _directoryDetails = value;
                base.RaisePropertyChanged("DirectoryDetails");
            }
        }
    }

    private DirectoryModel _selectedDirName;

    public DirectoryModel SelectedDirName
    {
        get
        {
            return _selectedDirName;
        }
        set
        {
            _selectedDirName = value;
            base.RaisePropertyChanged("SelectedDirName");
        }
    }
}

请让我知道,我做错了?

Please let me know, what am I doing wrong?

谢谢,
AG

Thanks,AG

推荐答案

p>很难说没有看到XAML,但我的第一个想法是1)您没有将DataContext属性设置为ViewModel或2)您在Binding本身有一些语法问题。

Hard to say without seeing the XAML, but my first thoughts are either 1) you have not set the DataContext property to the ViewModel or 2) you have some syntax issue in the Binding itself.

您应该使用ObservableCollection而不是IEnumerable&DirectoryModel来支持DataBinding。我也不确定您的DirectoryDe​​tails getter的实现是否有益。您的设置器直接设置私有变量并触发PropertyChanged事件 - 这是正确的。但是您的getter也可以直接设置变量,而不需要PropertyChanged事件。更不用说你有一个吸烟者做一个设定者的工作,这可能是一个坏主意在几个级别。我想你会更好地简化你的getter,并让它只返回私有变量。你真的需要每次检索细节,还是使用局部变量?

You should use ObservableCollection instead of IEnumerable<DirectoryModel> to support DataBinding. I'm also not sure the implementation of your DirectoryDetails getter is beneficial. Your setter sets the private variable directly and fires the PropertyChanged event - this is proper. But your getter also sets the variable directly, bypassing the PropertyChanged event. Not to mention that you have a getter doing the work of a setter, which is probably a bad idea on several levels. I think you would be better to simplify your getter and have it just return the private variable. Do you really need to retrieve teh details everytime or can you use the local variable?

我还会指出,您不需要在您的Model上实现INotifyPropertyChanged:ViewModel需要此接口来支持DataBinding,但是在添加它时没有真正的价值到模型类。

I would also point out that you do not need to implement INotifyPropertyChanged on your Model: the ViewModel needs this interface to support DataBinding, but there is no real value in adding it to the Model class.

这篇关于主要细节MVVM WPF不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 03:32