Sowvik Roy
Sowvik Roy

Reputation: 913

How to bind string form List of String?

Here is the code for which the data binding is not working. XAML

<ListBox x:Name="RecentBox" SelectionChanged="RecentBox_SelectionChanged" ItemsSource="{Binding}"  >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border BorderBrush="Red" BorderThickness="1" Background="Blue">
                        <TextBlock Text="{Binding RecentItemList}"/>
                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

Now the CS code

public ObservableCollection<string> RecentItemsList { get { return new ObservableCollection<string>(((MainWindow)Application.Current.MainWindow).cacheFileList); } }
        public RecentItems()
        {
            InitializeComponent();
            RecentBox.ItemsSource = RecentItemsList;
        }

Now how to bind the string to the Textblock element?

Upvotes: 0

Views: 70

Answers (2)

Anshuman Khandelwal
Anshuman Khandelwal

Reputation: 1

RecentItemList use in Listbox itemsource property and use like this:

<Border BorderBrush="Red" BorderThickness="1" Background="Blue"> <TextBlock Text="{Binding}"/> </Border>

Upvotes: 0

Sajeetharan
Sajeetharan

Reputation: 222720

If the items source is enumerable as string-entries, use the following:

<TextBlock Text="{Binding}"></TextBlock> 

XAML:

  <ListBox x:Name="RecentBox" SelectionChanged="RecentBox_SelectionChanged" ItemsSource="{Binding RecentItemList}"  >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border BorderBrush="Red" BorderThickness="1" Background="Blue">
                    <TextBlock Text="{Binding}"/>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

Upvotes: 2

Related Questions