1
votes

I'm binding my ListBox to an ObservableCollection at runtime. Upon clicking a button, one of the items in my collection gets modified, but the corresponding ListBox item doesn't update itself accordingly. I have gone thru several similar SO articles and other help material and it seems like I'm doing everything they've asked for, but no luck. Everything seems to load and bind correctly, but when I change "IsLoading" property of an item in my collection, the Visibility of the Grid (see DataTemplate below) which is bound to IsLoading property doesn't change.

Following is my ListBox XAML:

        <ListBox Name="lstItems">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Name="ListBoxGrid">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="120"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="100"/>
                        </Grid.ColumnDefinitions>
                        <CheckBox Grid.Column="0" IsChecked="{Binding IsSelected}" />
                        <Image Grid.Column="1" Width="50" Stretch="Uniform" Source="{Binding Image}" />
                        <TextBlock Grid.Column="2" Text="{Binding Path=ImageFilePath}" />
                        <Grid Grid.Column="3" Visibility="{Binding IsLoading, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, Mode=TwoWay, BindsDirectlyToSource=True, Converter={StaticResource BooleanToVisibilityConverter1}}">
                            <my:LoadingAnimation x:Name="SendAnimation" VerticalAlignment="Center" HorizontalAlignment="Center" />
                        </Grid>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

And here's my BO:

public class Order : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public bool IsSelected { get; set; }
    public string ImageFilePath { get; set; }
    public ImageSource Image { get; set; }

    private bool mIsSending = false;
    public bool IsSending
    {
        get { return mIsSending; }
        set
        {
            mIsSending = value;

            if (PropertyChanged != null)
                PropertyChanged(null, new PropertyChangedEventArgs("IsSending"));
        }
    }
}

And this is how I create the collection and bind it:

    ObservableCollection<Order> mOrders = new ObservableCollection<Order>();

    public MainWindow()
    {
        InitializeComponent();

        lstItems.ItemsSource = mOrders;
    }
1
what if you set BindsDirectlyToSource=False?Klaus78
@Klaus78: Thanks Klaus. See my answer below. I also figured out that none of the other binding attributes are required. So you can have it just like Visibility="{Binding IsSending, Converter={StaticResource BooleanToVisibilityConverter1}}".dotNET

1 Answers

2
votes

Nevermind. Sometimes it is that you spend hours digging the problem, finally get frustrated, post it on SO, and the next 2 minutes you figure it out yourself. For any future reader, the only problem was that I was sending null in PropertyChanged event. As soon as I changed that to this, things started working like a charm.