开发者

Update a label from datacontext

MyView.xaml

<UserControl x:Class="CCTrayHelperWPF.View.StatusView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/bl开发者_Go百科end/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="30" d:DesignWidth="1200">
<Grid >
    <Label Content="{Binding Message}">
    </Label>
</Grid>

StatusViewModel .cs

 public class StatusViewModel : ViewModelBase   --> this class has inherited INotifyPropertyChanged
{
     ObservableCollection<Status> _statusData;
    public StatusViewModel()
    {
         this._statusData = new ObservableCollection<Status>();

     }

    public ObservableCollection<Status> ProjectStatus
    {
        get { return _statusData; }
    }
}

Status.cs

public class Status : ViewModelBase
    {
        private string _message;
        public string Message
        {
            get { return _message; }
            set
            {
                if (_message == value) return;
                _message = value;
                OnPropertyChanged("Message");
            }
            }
        }

In main window where I intergrated the view usercontrol giving the dataContext

MainWindow.xaml.cs

         if (!DesignerProperties.GetIsInDesignMode(this))
        {
            StatusViewModel statusModel = new StatusViewModel(controller);
            this.StatusView.DataContext = statusModel;
        }

Now my question is: Why am I seein this binding error? Error is: BindingExpression path error: 'Message' property not found on 'object' ''StatusViewModel' (HashCode=44528608)'. BindingExpression:Path=Message; DataItem='StatusViewModel' (HashCode=44528608); target element is 'Label' (Name=''); target property is 'Content' (type 'Object')


There is a lot of stuff going on here and some good responses already. As said before, the first problem is that your DataContext object type does not define a property called Message.

The second problem is that you are trying to use a single property from an ObservableCollection. How do you intend for the Label to know which item in the collection it should be binding to?

There is a third concern I have: why are you using a Label? Labels in WPF/SL are not the same as in WinForms and WebForms. They are heavyweight wrappers around a TextBlock that provide keyboard directionality to a target field (in other words, if the Label was FirstName you could use the keyboard to go to the related FirstNameTextBox). If all you want to do is present readonly text on the screen, use TextBlock instead.


If you are following the MVVM pattern you should be able to bind the label to number of items in the collection of your viewmodel.


From the code you have put up it looks like you are binding against the wrong class. Your Status class has a Message property but you are setting your DataContext to an object of type StatusViewModel, which doesn't have a Message property. You're then binding the label to the property Message, which doesn't exist. Either set your DataContext to a Status object or surface the Status you want in your StatusViewModel.


You have a property Message on your class Status, but not your class StatusViewModel.

You can either add the property to the StatusViewModel, change StatusView.DataContext to be Status or change your binding to:

<Label Content="{Binding ProjectStatus.Message}">

where you are binding to a property of a property.


You're question is not totally clear. I hope you know a Grid is different from a GridView and is NOT a Grid of items.

If you are trying to display a LIST of status's then you will do:

<ListBox x:Name="myList" ItemsSource={Binding ProjectStatus}> 
   <ListBoxItem>
     <DataTemplate>
       <TextBlock Text="{Binding Message}"></TextBlock></DataTemplate>
       //I assume you want a textblock but you can use a label.
    </ListBoxItem>
</ListBox>

Now just set the DataContent of your listbox to an instance of the ProjectStatus class. It should work.

myList.DataContext = new StatusViewModel();


if you do mvvm, you have the loaddata command where you populate your collection(which is bind to your gridview). so all you have to do now is create a second property "Itemsnumber" or whatever and set it to the collection.count property. you do this all in your loadcommand. if you implement INotifyPropertyChangeed properly and your binding are correct, you will see your label updated.

Edit: SelectedItems if you need the selecteditems in your vm then you should google for "SeletedItems MVVM" there are a plenty of possible solutions out there (AttachedProperties, Behaviors, MultiSelectControls...)

if you just want to show a label on your ui, just use the selectionchanged event.

    private void MyGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        this.MySelectedItemsTextBlock.Text = this.MyGrid.SelectedItems.Count;
    }

EDIT: this question changes totally, so ignore my anwser for your new question:)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜