简体   繁体   中英

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();

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>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM