Ahmet Can Güven
Ahmet Can Güven

Reputation: 5462

Listbox Itemsource binding not working

I am trying to bind my list to a listbox. Listbox binding is working but I can't bind property to item template.

Here is my class

public class handle{
        public string smurfName;
        internal Process process;

        public handle(Process _pro, string _smuf)
        {
            process = _pro;
            smurfName = _smuf;
        }
    }

Here is my window constructor.

public static ObservableCollection<handle> smurfList = new ObservableCollection<handle>();
    public GameMask()
        {
            InitializeComponent();
            runningSmurfs.ItemsSource = smurfList;

            handle newSmurf = new handle(null, "THISNAME");
            smurfList.Add(newSmurf);
        }

Here is my xaml

<ListBox HorizontalAlignment="Left" Height="200" Margin="3,41,0,0" VerticalAlignment="Top" Width="113" Name="runningSmurfs">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=smurfName}"></TextBlock>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

Unfortunetely I can't see "THISNAME" on the list, but if I change Texblock Binding to a text it is working fine.

Thank you for your helps.

Upvotes: 0

Views: 94

Answers (3)

Othman Dahbi-Skali
Othman Dahbi-Skali

Reputation: 632

In your handle class add a getter and setter to your smurfName property:

public string smurfName { get; set; }

Upvotes: 0

kirotab
kirotab

Reputation: 1306

In your case you don't need to implement INotifyPropertyChanged I think, but you can't bind to a field which you were trying to do

Try this:

public string SmurfName { get; set; }

and change in xaml the binding to SmurfName

Upvotes: 1

Giangregorio
Giangregorio

Reputation: 1510

You need at least a property so your binding can work. smurfName should be a property, better if you implement INotifyPropertyChanged too.

Upvotes: 2

Related Questions