0

我正在尝试将一个组合框添加到一个已经存在的 WPF 表单中。我已经在 XAML 中创建了组合框代码,并在我的 ViewModel 类中添加了适当的代码。但是,当我尝试将我的组合框项目添加到我的 ObservableCollection 时,我收到以下错误:

'AECI.Clio.StartPageControl.ClioViewModel.Groups' is a 'property' but is used like a 'type'   

这是我的组合框的 XAML 代码:

<ComboBox Name="GroupComboBox" ItemsSource="{Binding Groups}" SelectedItem="{Binding MySelectedItem, Mode=TwoWay}" Width="132" Height="22" Grid.Column="0" Grid.Row="3" Margin="0,30" VerticalAlignment="Top" >    </ComboBox>

这是我的 C# 代码:

public class ClioViewModel : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)  
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    } 

    private ObservableCollection<string> _groups = new ObservableCollection<string>();

    public ObservableCollection<string> Groups 
    { 
        get { return _groups; } set { _groups = value; OnPropertyChanged("Groups");   }   
    } 

Groups.Add("Data Warehouse"); <-- Here is where I get the error 

我不知道我做错了什么。请指教。

编辑:

根据要求,这是全部。我没有从一开始就发布整个内容,因为它非常大。

namespace AECI.Clio.StartPageControl
{
    public class ClioViewModel : INotifyPropertyChanged
    {
        private Timer _timer;

        public ClioViewModel()
        {
            Harvesters = new ObservableCollection<Harvester>();
        _   timer = new Timer((o) => Application.Current.Dispatcher.BeginInvoke(new Action(LoadAllHarvester)), new object(), TimeSpan.FromMilliseconds(1), TimeSpan.FromMinutes(1));           
    }

    private void LoadAllHarvester()
    {
        Harvesters.Clear();
        foreach(var h in HarvesterModel.GetAllHarvesters())
        {
            Harvesters.Add(h);
        }
    }

    public ObservableCollection<Harvester> Harvesters { get; set; }

    private Harvester _selectedHarvester;

    public Harvester SelectedHarvester
    {
        get { return _selectedHarvester; }
        set
        {
            _selectedHarvester = value;
            if (_selectedHarvester != null)
            {
                _selectedHarvester.PropertyChanged += _selectedHarvester_PropertyChanged;
            }
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("SelectedHarvester"));
                PropertyChanged(this, new PropertyChangedEventArgs("Selected"));
                PropertyChanged(this, new PropertyChangedEventArgs("SelectedAndNotLoading"));
            }
        }
    }

    private DateTime _harvestDateStart = DateTime.Today;
    public DateTime HarvestDateStart
    {
        get { return _harvestDateStart; }
        set
        {
            _harvestDateStart = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("HarvestDateStart"));
            }
        }
    }

    void _selectedHarvester_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs("SelectedHarvester"));
        }
    }

    public bool Selected
    {
        get
        {
            return SelectedHarvester != null;
        }
    }

    public bool SelectedAndNotLoading { get { return Selected && !PostOccuring; } }

    public bool HarvesterPostRequested { get { return false; } set { PostSelectedHarvester(); } }
    public bool HarvesterPauseRequested { get { return false; } set { PauseSelectedHarvester(); } }
    public bool HarvesterUnpauseRequested { get { return false; } set { UnpauseSelectedHarvester(); } }


    private bool _postOccuring;

    public bool PostOccuring
    {
        get { return _postOccuring; }
        set
        {
            _postOccuring = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("PostOccuring"));
                PropertyChanged(this, new PropertyChangedEventArgs("SelectedAndNotLoading"));
            }
        }
    }

    private void PostSelectedHarvester()
    {
        PostOccuring = true;
        HarvesterModel.UpdateHarvester(SelectedHarvester);
        PostOccuring = false;
    }

    private void UnpauseSelectedHarvester()
    {
        if (SelectedHarvester != null)
        {
            SelectedHarvester.NextRunTimeUTC = DateTime.UtcNow;
            PostSelectedHarvester();
            SelectedHarvester.CanPause = true;
            SelectedHarvester.CanResume = false;
        }
    }

    private void PauseSelectedHarvester()
    {
        if (SelectedHarvester != null)
        {
            SelectedHarvester.NextRunTimeUTC = null;
            PostSelectedHarvester();
            SelectedHarvester.CanPause = false;
            SelectedHarvester.CanResume = true;
        }
    }

    public bool HarvestRequested { get { return false; } set { HarvestDataNow(); } }

    public void HarvestDataNow()
    {
        var t = HarvestDateStart;
        if (t > DateTime.Now || SelectedHarvester == null)
        {
            return;
        }
        PostOccuring = true;
        HarvesterModel.HarvestDataNow(SelectedHarvester, t, DateTime.Now);
        PostOccuring = false;
    }

    #region Implementation of INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    protected void OnPropertyChanged(string propertyName)  
    {
        PropertyChangedEventHandler handler = PropertyChanged;
      if (handler != null)
      {
          handler(this, new PropertyChangedEventArgs(propertyName));
      }
    }

    private ObservableCollection<string> _groups = new ObservableCollection<string>();

    public ObservableCollection<string> Groups 
    { 
        get 
        { 
            return _groups; 
        }
        set
        {
            _groups = value;
            OnPropertyChanged("Groups"); 
        }            
    }
    Groups.Add("Data Warehouse");        

    private ObservableCollection<string> _selectedGroups = new ObservableCollection<string>();

    public ObservableCollection<string> SelectedGroups 
    { 
        get
        {
            return _selectedGroups;
        }
        set
        {
            _selectedGroups = value;
            OnPropertyChanged("SelectedGroups");            
        }
    }        

    private string _mySelectedItem;
    public string MySelectedItem
    {
        get{return this._mySelectedItem;}
        set{this._mySelectedItem=value; OnPropertyChanged("MySelectedItem");}
    }      

}

public static class HarvesterModel
{
    private static readonly EndpointAddress ENDPOINT = new EndpointAddress(new Uri("http://LocalHost:49350/POWDataWarehouseFeeder/V1/Silverlight"));
    private static readonly BasicHttpBinding BINDING;

    static HarvesterModel()
    {
        BINDING = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
        BINDING.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
        BINDING.SendTimeout = TimeSpan.FromMinutes(5);
        BINDING.ReceiveTimeout = TimeSpan.FromMinutes(5);
    }

    private static Dictionary<Guid, Clio.HarvesterRecord> Harvesters { get; set; }

    public static void HarvestDataNow(Harvester harvey, DateTime stime, DateTime etime)
    {
        var client = new Clio.ClioManagerV1Client(BINDING, ENDPOINT);

        try
        {
            client.HarvestDataImmediate(new DataHarvestTarget[] { new DataHarvestTarget { EndTimeUTC = etime.ToUniversalTime(), StartTimeUTC = stime.ToUniversalTime(), HarvesterGuid = harvey.HarvesterGuid.ToString(), EndTimeUTCSpecified = true, StartTimeUTCSpecified = true } });
        }
        catch {}

    }

    public static ICollection<Harvester> GetAllHarvesters()
    {
        var client = new Clio.ClioManagerV1Client(BINDING, ENDPOINT);
        HarvesterRecord[] harvs;
        try
        {
            harvs = client.GetHarvesters();
        }
        catch(Exception e)
        {
            harvs = new HarvesterRecord[0];
        }
        foreach (var record in harvs)
        {
            record.HarvesterConfigurationXML.Attributes.RemoveAll();
        }
        Harvesters = harvs.ToDictionary(n => new Guid(n.HarvesterGuid), n => n);

        return
                    harvs.Select(
                                n =>
                                new Harvester
                                    {
                                        Configuration = XElement.Parse(n.HarvesterConfigurationXML.OuterXml).ToString(),
                                        HarvesterGuid = new Guid(n.HarvesterGuid),
                                        HarvesterName = n.HarvesterName,
                                        LastRunMessage = n.LastRunMessage,
                                        LastRunSuccess = n.LastRunResult == HarvestResults.Success,
                                        LastRunTimeUTC = n.LastRunTimeUTC,
                                        NextRunTimeUTC = n.NextScheduledRunTimeUTC,
                                        CanPause = n.LastRunTimeUTC != null,
                                        CanResume = n.LastRunTimeUTC == null,
                                        Schedules = new List<HarvesterSchedule>(n.HarvesterRunScheduleList.Select(h => new HarvesterSchedule { Schedule = h.Schedule, Window = System.Xml.XmlConvert.ToTimeSpan(h.WindowLength), Offset = System.Xml.XmlConvert.ToTimeSpan(h.Offset) })),
                                    }).ToList();
    }

    public static void UpdateHarvester(Harvester harvey)
    {
        var old = Harvesters[harvey.HarvesterGuid];
        var oldPost = new Clio.HarvesterPost
                          {
                              GACFullAssemblyName = old.GACFullAssemblyName,
                              HarvesterConfigurationXML = old.HarvesterConfigurationXML,
                              HarvesterGuid = old.HarvesterGuid,
                              HarvesterName = old.HarvesterName,
                              HarvesterRunScheduleList = old.HarvesterRunScheduleList
                          };
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(harvey.Configuration);
        var newPost = new Clio.HarvesterPost
                          {
                              GACFullAssemblyName = old.GACFullAssemblyName,
                              HarvesterConfigurationXML = doc.DocumentElement,
                              HarvesterGuid = old.HarvesterGuid,
                              HarvesterName = harvey.HarvesterName,
                              IsExeucting = harvey.NextRunTimeUTC != null,
                              IsExeuctingSpecified = true,
                              HarvesterRunScheduleList = harvey.Schedules.Select(s => new Clio.HarvesterRunSchedule { Schedule = s.Schedule, Offset = System.Xml.XmlConvert.ToString(s.Offset), WindowLength = System.Xml.XmlConvert.ToString(s.Window) }).ToArray()
                          };

        var client = new Clio.ClioManagerV1Client(BINDING, ENDPOINT);
        var result = client.PostHarvesters(new[] { new HarvesterPostRequest { Current = oldPost, Updated = newPost } });
        return;
    }
}

public class Harvester : INotifyPropertyChanged
{
    public Guid HarvesterGuid { get; set; }
    public string HarvesterName { get; set; }
    public DateTime LastRunTimeUTC { get; set; }
    public DateTime LastRunTimeLocal { get { return LastRunTimeUTC.ToLocalTime(); } }
    public DateTime? NextRunTimeUTC { get; set; }
    public DateTime? NextRunTimeLocal { get { return (NextRunTimeUTC ?? DateTime.MinValue).ToLocalTime(); } }
    public bool LastRunSuccess { get; set; }
    public string LastRunMessage { get; set; }
    public string Configuration { get; set; }
    public ICollection<HarvesterSchedule> Schedules { get; set; }

    private bool _canPause;

    public bool CanPause
    {
        get { return _canPause; }
        set
        {
            _canPause = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("CanPause"));
            }
        }
    }
    private bool _canResume;

    public bool CanResume
    {
        get { return _canResume; }
        set
        {
            _canResume = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("CanResume"));
            }
        }
    }

    private bool _editXML;

    public bool EditXML
    {
        get { return _editXML; }
        set
        {
            _editXML = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("EditXML"));
            }
        }
    }

    #region Implementation of INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}

public class HarvesterSchedule : INotifyPropertyChanged
{

    private string _schedule;

    public string Schedule
    {
        get { return _schedule; }
        set
        {
            _schedule = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Schedule"));
            }
        }
    }

    private TimeSpan _offset;

    public TimeSpan Offset
    {
        get { return _offset; }
        set
        {
            _offset = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Offset"));
            }
        }
    }

    private TimeSpan _window;

    public TimeSpan Window
    {
        get { return _window; }
        set
        {
            _window = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Window"));
            }
        }
    }

    #region Implementation of INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}

public class SuccessBoolToColorConverter : IValueConverter
{
    #region Implementation of IValueConverter

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is bool && (bool)value)
        {
            return new SolidColorBrush(Colors.AliceBlue);
        }
        return new SolidColorBrush(Colors.Tomato);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is Color && (Color)value == Colors.AliceBlue)
        {
            return true;
        }
        return false;
    }

    #endregion
}

public class BoolToVisibilityConverter : IValueConverter
{
    #region Implementation of IValueConverter

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is bool && (bool)value)
        {
            return Visibility.Visible;
        }
        return Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is Visibility && (Visibility)value == Visibility.Visible)
        {
            return true;
        }
        return false;
    }

    #endregion
}

public class HarvesterToCheckedConverter : IValueConverter
{
    #region Implementation of IValueConverter

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
        {
            return true;
        }
        return false;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }

    #endregion
}

public class HarvesterLoading : IValueConverter
{
    #region Implementation of IValueConverter

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if(value is bool && (bool)value)
        {
            return 0.0;
        }
        return 1.0;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}
4

3 回答 3

2

Groups.Add("Data Warehouse");

位置不正确(目前在类定义级别)。将其粘贴到某个方法或设置器中,它将开始工作。

于 2012-07-19T16:17:03.060 回答
1

您的 Groups.Add 需要在某个方法的内部。现在,因为它没有包含在任何方法中,所以它被视为一个静态类。将 Groups.Add 行移到您的构造函数中。

于 2012-07-19T16:18:43.510 回答
1

您应该将 Groups.Add(...) 放在构造函数中。

public ClioViewModel()
{
    Harvesters = new ObservableCollection<Harvester>();
    _timer = new Timer((o) => Application.Current.Dispatcher.BeginInvoke(new Action(LoadAllHarvester)), new object(), TimeSpan.FromMilliseconds(1), TimeSpan.FromMinutes(1));
    Groups.Add("Data Warehouse");
}
于 2012-07-19T16:21:46.973 回答