在ViewModel问题中绑定到Object属性

时间:2016-03-07 20:02:01

标签: c# wpf mvvm datacontext access-modifiers

不确定为什么这不起作用...下面我是我的ViewModel,它被设置为我的View DataContext。

public class UploadViewModel : CrudVMBase
    {
        #region Commands
        public CommandVM UploadButtonCommand { get; set; } =
            new CommandVM
            {
                CommandDisplay = "Perform Upload",
                IconGeometry = App.Current.Resources["pencil30"] as Geometry,
                Message = new CommandMessage { Command = CommandType.UploadFromCamera }
            };
        #endregion End Commands

        #region Public Properties
        UploadInitiation UploadObject { get; set; } = new UploadInitiation();
        #endregion End Public Properties

        public UploadViewModel()
        {

        }

以下是UploadInitiation类

public class UploadInitiation : Common.NotifyUIBase
    {
        #region Public Properties
            public ObservableCollection<UploadStep> Steps { get; set; } = new ObservableCollection<UploadStep>();
            public int UploadProgress { get; set; } = 45;
            public string UploadTask { get; set; } = "Idle...";
            public bool UploadEnabled { get; set; } = false;
            public bool UploadBegin { get; set; } = false;
        #endregion END Public Properties

        public UploadInitiation()
        {
            // Populate steps required, ensure upload returns UI updates
            Steps.Add(new UploadStep { Message = "Seperate upload to new thread...", Complete = false, Error = null });
            Steps.Add(new UploadStep { Message = "Generate new file names...", Complete = false, Error = null });
            Steps.Add(new UploadStep { Message = "Render Thumbnails, add to database...", Complete = false, Error = null });
            Steps.Add(new UploadStep { Message = "Move images ready for print...", Complete = false, Error = null });
        }
    }

这是我的Binding,因为你可以看到我试图绑定到UploadProgress属性。

<ProgressBar Style="{StaticResource CircularProgress}" Width="180" Value="{Binding UploadObject.UploadProgress}" />

这是错误

  

System.Windows.Data错误:40:BindingExpression路径错误:   'object'''UploadViewModel'上找不到'UploadObject'属性   (的HashCode = 33902366)”。   BindingExpression:路径= UploadObject.UploadProgress;   DataItem ='UploadViewModel'(HashCode = 33902366);目标元素是   'ProgressBar'(Name =''); target属性为'Value'(类型'Double')

1 个答案:

答案 0 :(得分:1)

您需要将属性的范围声明为 public ,否则默认情况下它将是私有的。因此,绑定时不可见。

public UploadInitiation UploadObject { get; set; } = new UploadInitiation();