I have a public class LPosition
, which contains an attribute public String OZ { get; set; }
. The application reads .txt and .xml files and OZ gets the values from these files. I need to bind OZ to comboBox:
<ComboBox x:Name="OZs" SelectionChanged="OZs_SelectionChanged" Margin="-10,0,10,1" Grid.Column="0" Grid.Row="1" Height="27" VerticalAlignment="Bottom">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding OZ}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
As you see, I have tried to implement data binding using DataTemplate, but it doesn't work. I know that it's possible to implement data binding with ItemsSource, but for this I need an ObservableCollection, which I don't have and I don't know how to create it from OZ. I have seen many examples where an ObservableCollection is hard coded, and I understand how to use it when it's hard coded, but I have no idea what to do in my case.
Sorry if the explanation is not clear, I'm very new to WPF. And I'm very lost with this problem. Any help would be appreciated.
edit: according to the answer of @Xiaoy312 I've added following code:
public IEnumerable<LPosition> OZList { get; set; }
public FormelAssistent()
{
InitializeComponent();
this.DataContext = this;
OZs.ItemsSource = OZList;
}
and XAML:
<ComboBox x:Name="OZs"
SelectionChanged="OZs_SelectionChanged"
Margin="-10,0,10,1"
Grid.Column="0"
Grid.Row="1"
Height="27"
VerticalAlignment="Bottom"
ItemsSource="{Binding OZList}"
DisplayMemberPath="OZ" />
But it doesn't work :(