views:

830

answers:

3

Hi, I'm stumped here. I have an observable collection that holds business objects. I have it bound to ItemsSource of a ListBox. I am updating the X and Y of my object and it is being displayed correctly in the UI during runtime as it is bound the the Item top and Left. But, here is where the problem is. I have also bound some data to be displayed in textblock text property and the data only displays the initial value. It never updates the textblock Text no matter how many times I change it.

Here is the XAML. If you see a problem with the XAML please let me know. Like I said, the X/Y - Top/Left binding works just fine and updates when changed, the TextBlock that is bound to DisplayData does not.

Also, my business object in my collection does Implement INotifyPropertyChanged.

I will try to make a small demo to replicate this if an answer can not be given just by looking at the XAML.

Thanks in advance!

<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>

A: 

Your business objects need to implement the INotifyPropertyChanged interface, so that the UI is notified of the change and can update to reflect the new value

Thomas Levesque
Yes it does, I stated that in the above.
Doug
+2  A: 

Hi Doug,

I think the problem is in the code behind. Your XAML is absolutely legal and looks good. But before you post the source code make sure the following conditions are true:

  1. Your business object implements INotifyPropertyChanged interface and you raise PropertyChanged event every time DisplayData value is changed.

  2. There are no typos. Neither in DipslayData property definition nor in the PropertyChangedEventArgs, where you pass "DispayData" property name.

  3. DataContext of a ListBoxItem is of your business object's type. Check it with Snoop.

  4. There are no binding errors in runtime. Run your application in debug and check your Output window. You can also check this with Snoop.

Hope after completing this check list you'll have the answer.

Cheers :)

Anvaka
Anvaka, Thanks for looking at this.1. Yes.2. Checked and OK.3. Did not check that yet.4. No binding errors.I created a small application to replicate this situation and the small application works perfectly. So you're absolutely right, it MUST be in my codebehind. I'll scour the code and report back.Thanks again.
Doug
Anvaka you were right. The issue was in my codebehind. A STUPD, STUPID problem. I was never updating DisplayData after Sub New. DOH! Sorry if I wasted your time.Doug
Doug
I'm glad you found the answer :). Congratulations!
Anvaka
A: 

Uhm, the DataContext inside the ListBox is an item of the ItemsSource list.

for example, if you ListBox is binded to a ObservableCollection, the DataContext inside the ListBox will be a Person object and not the parent's datacontext.

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

I don't know the behavior without a ItemsSource.

Maybe you know this, but maybe this helps you.

Jesus Rodriguez