当列表数据绑定改怎么通知XAML属性? [英] How to notify XAML properties when list data of binding changed?

查看:108
本文介绍了当列表数据绑定改怎么通知XAML属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code的结合

I'm using the following code for binding

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

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

这code将通知到XAML更新,当我使用code的行

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

listDATA[0].ChannelRecordTimeItemWidth -= 15;

有什么办法XAML更新属性自动,当我们操作上的listData为

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

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

无需调用以下两个行code

Without calling the two following lines code

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

感谢您!

T&amp; T公司

T&T

推荐答案

GUI只会在情况更新源集合正在实施INotifyCollectionChanged 由此引发潜在的 Col​​lectionChanged 事件刷新GUI组件。

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

您可以使用的ObservableCollection 它在内部为您提供此功能。

You can use ObservableCollection which internally provides you this feature.

替换

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

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

这篇关于当列表数据绑定改怎么通知XAML属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆