Tooly
Tooly

Reputation: 35

WPF ContextMenu - no String with ItemTemplate

Hi I am new to WPF Development and run into a problem Regarding Binding al public variable to a TextBlock element.

<ListBox.ContextMenu>
   <ContextMenu ItemsSource="{Binding ActionsView}">
      <ContextMenu.ItemTemplate>
          <DataTemplate>                                
            <TextBlock Text="{Binding Name}" />
          </DataTemplate>
      </ContextMenu.ItemTemplate>    
   </ContextMenu>

Action View is a public Observable collection of Action Items each holds a name which is public accessible as Name. So normally there should be no Problem. If I am right clicking on my Item, I get an empty ContextMenu with the correct number of entry’s but without any text.

picture of the empty ContextMenu

public class Action : INotifyPropertyChanged
{
    public string Name;        

    public ContextAction(string name)
    {
        Name = name;            
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

It would be really nice if somebody could help me with this problem.

Upvotes: 0

Views: 231

Answers (2)

Tooly
Tooly

Reputation: 35

The solution was setting the getters an setters that’s it :)

 public class ContextAction : INotifyPropertyChanged
{
    public string _name;   

    public ContextAction(string name)
    {
        _name = name;            
    }


    public string Name
    {
        get { return _name; }
    }

    public event PropertyChangedEventHandler PropertyChanged;

}

Upvotes: 1

Matas
Matas

Reputation: 860

You need to implement property, not a field for bindings to work. Like this:

public string Name { get; set };

Upvotes: 0

Related Questions