TTGroup
TTGroup

Reputation: 3703

How to notify XAML properties when list data of binding changed?

I'm using the following code for binding

XAML

<StackPanel x:Name="channelsRecordTimeData" Orientation="Vertical">
    <ItemsControl x:Name="channelRecordTimeItems" ItemsSource="{Binding}">
        <ItemsControl.ItemTemplate> 
            <DataTemplate>
                <Grid x:Name="gridChannelRecordTimeItem" Width="{Binding Path=ChannelRecordTimeItemWidth}"                                                                                                                
                      Height="{Binding Path=ChannelRecordTimeItemHeight}" Margin="{Binding Path=ChannelRecordTimeItemsMargin}"
                        HorizontalAlignment="Left" DataContext="{Binding Path=ListRecordTime}">
                    <Grid.Background>
                        <ImageBrush x:Name="gridChannelRecordTimeItemBgr" ImageSource="..\Resources\playback_grid_channel_record_time_item_bgr_normal.png"/>
                    </Grid.Background>                                    
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</StackPanel>

C#

public class DATA
{
    public double ChannelRecordTimeItemWidth { set; get; }
    public double ChannelRecordTimeItemHeight { set; get; }
    public Thickness ChannelRecordTimeItemsMargin { set; get; }
    public List<RecordTime> ListRecordTime { set; get; }

    public DATA()
    {
        ChannelRecordTimeItemWidth = 1000;
        ChannelRecordTimeItemHeight = 20;
        ChannelRecordTimeItemsMargin = new System.Windows.Thickness(0, 0, 0, 0);
        ListRecordTime = null;
    }
}

public static List<DATA> listDATA = new List<DATA>();
for(int i = 0 ; i < 10 ; i++)
{
    DATA data = new DATA();
    listDATA.Add(data);
}
channelRecordTimeItems.ItemsSource = listDATA;
channelRecordTimeItems.Items.Refresh();

This code will notify to the XAML update when I use the line of code as

listDATA[0].ChannelRecordTimeItemWidth -= 15;

There is any way to XAML update properties automatically, when we manipulate on the listDATA as

listDATA.RemoveAt();
listDATA.Add();
listDATA.Clear();

Without calling the two following lines code

channelRecordTimeItems.ItemsSource = listDATA;
channelRecordTimeItems.Items.Refresh();

Upvotes: 0

Views: 1358

Answers (1)

Rohit Vats
Rohit Vats

Reputation: 81243

GUI will be updated only in case underlying source collection is implementing INotifyCollectionChanged which raise CollectionChanged events to refresh GUI components.

You can use ObservableCollection which internally provides you this feature.

Replace

public static List<DATA> listDATA = new List<DATA>();

with

public static ObservableCollection<DATA> listDATA = new ObservableCollection<DATA>();

Upvotes: 1

Related Questions