Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple WPF Binding not working

I'm learning WPF, and I'm stuck with Data Bindings. I have a TreeView, which ItemSource is set to a ObserveableCollection<UIBrowserItem>.

My Binding looks like:

<TreeView>
    <TreeView.ItemContainerStyle>
        <Style TargetType="TreeViewItem">
            <Setter Property="Header" Value="{Binding Path=Title}"/>
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>

And my UIBrowserItem is very basic:

public class UIBrowserItem 
{
    public string Title = "Test";
}

But the Items in the TreeView won't have a header set ..

If you need further information, tell me

like image 846
tobspr Avatar asked Oct 24 '25 08:10

tobspr


1 Answers

You can only bind to public properties, you have a public field. Your code should be:

public class UIBrowserItem 
{
    private String title = "Test";
    public string Title
    {
       get { return title; }
       set { title = value; }
}

If the title can change at run time, you also need to implement INotifyPropertyChanged.

like image 162
BradleyDotNET Avatar answered Oct 26 '25 23:10

BradleyDotNET