0

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?

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

Comments

0

RecentItemList use in Listbox itemsource property and use like this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.