Items in ObservableCollection do not update view

I'm here. I have an observable collection that contains business objects. I linked it to the ItemsSource ListBox. I update the X and Y of my object and it displays correctly in the UI at runtime as it is associated with the top and left element. But here's the problem. I have also linked some data to be displayed in the text property textblock and the data will only display the initial value. It never updates the Text block no matter how many times I change it.

Here is the XAML. If you see a problem with the XAML, please let me know. As I said, X / Y - Top / Left binding works fine and updates on change, TextBlock bound to DisplayData doesn't work.

Also, my business object in my collection implements INotifyPropertyChanged.

I will try to do a small demo to reproduce this if the answer cannot be given simply by looking at the XAML.

<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="614" Width="674">

<ListBox Name="PlottingBox" Background="White">

    <ListBox.ItemTemplate>

        <DataTemplate>

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

        </DataTemplate>

    </ListBox.ItemTemplate>

    <ListBox.Template>

        <ControlTemplate TargetType="{x:Type ListBox}">

            <Border Background="{TemplateBinding Background}"

                BorderBrush="{TemplateBinding BorderBrush}"

                BorderThickness="{TemplateBinding BorderThickness}">

                <Canvas IsItemsHost="True" />

            </Border>

        </ControlTemplate>

    </ListBox.Template>

    <ListBox.ItemContainerStyle>

        <Style TargetType="{x:Type ListBoxItem}">

            <Setter Property="Canvas.Left" Value="{Binding Path=PlotX}" />

            <Setter Property="Canvas.Top" Value="{Binding Path=PlotY}" />

         </Style>

    </ListBox.ItemContainerStyle>

</ListBox>

      

+2


source to share


3 answers


I think the problem is in the code. Your XAML is completely legal and looks good. But before submitting the source code, make sure the following conditions are met:

  • Your business object implements the INotifyPropertyChanged interface and , each time you add a PropertyChanged event when the DisplayData value changes.

  • No typos. Neither in the DipslayData property definition, nor in the PropertyChangedEventArgs where you pass the property name "DispayData".

  • The DataContext of the ListBoxItem is of the type of your business object. Check it out with Snoop .

  • There are no binding errors in the runtime. Run the application in debug and check the output window. You can also check this with Snoop.



Hopefully after completing this checklist, you will receive a response.

Cheers :)

+5


source


Your business objects must implement the interface INotifyPropertyChanged

for the UI to be notified of this change and can update to reflect the new value



+1


source


Uhm, DataContext inside ListBox is ItemsSource list item.

for example, if the ListBox is bound to an ObservableCollection, the DataContext inside the ListBox will be a Person object, not the parent datacontext.

If you have a TextBlock with a binding, the binding will point to the Person object, in other words, personInstance.DisplayData, not parentDC.DisplayData.

I don't know the behavior without ItemsSource.

You may know this, but maybe it will help you.

0


source







All Articles