简体   繁体   中英

How to Update ItemsControl explicitly

I'm new to WPF (1 and a half weeks ), I've read and practiced as much as I can and in one of my practice exercises I encountered a problem that I haven't been able to resolve,

I want to let you know that I already know of the ObservableCollection but I can not use it since I realize many changes to the list before it is ready to be shown so this is why I decided to make the binding explicit:

 <ItemsControl  Background="Transparent" BorderBrush="Black" BorderThickness="1" Name="elementContainer" >
            <ItemsControl.ItemsSource>
                <Binding  UpdateSourceTrigger="Explicit" Mode="OneWay"  diag:PresentationTraceSources.TraceLevel="High" />
            </ItemsControl.ItemsSource>
                <ItemsControl.ItemContainerStyle>
                    <Style TargetType="ContentPresenter">
                        <Setter Property="Canvas.Left" Value="{Binding Rect.Left}" />
                        <Setter Property="Canvas.Top" Value="{Binding Rect.Top}" />
                    </Style>
                </ItemsControl.ItemContainerStyle>
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Canvas/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                            <Rectangle Width="{Binding Rect.Width}" Height="{Binding Rect.Height}" Stroke="#FFE01313" ></Rectangle>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

C# code:

List<Rect> mData;

DataContext = mData;

mData.Add(new Rect(20,20,20,20));

 var res = elementContainer.GetBindingExpression(ItemsControl.ItemsSourceProperty);
  res.UpdateTarget();

But all I got are this logs :

System.Windows.Data Warning: 101 : BindingExpression (hash=44489159): GetValue at level 0 from List`1 (hash=45943265 Count=3) using <null>: List`1 (hash=45943265 Count=3)
System.Windows.Data Warning: 80 : BindingExpression (hash=44489159): TransferValue - got raw value List`1 (hash=45943265 Count=3)
System.Windows.Data Warning: 89 : BindingExpression (hash=44489159): TransferValue - using final value List`1 (hash=45943265 Count=3)

I temporarily switched to ObservableCollection and it worked, so what am I missing ?

Thanks in Advance.

If you don't want to notify the ItemsControl because you have to make MANY changes, you can set the ItemsSource to null and then re-set it to its previous value. If you have to insert 1000+ items in your collection, this can offer better performance than having WPF update the view after each insert.

But as others have pointed out, you should try to use the ObservableCollection first.

You could also create your own class that implements INotifyCollectionChanged (for example by inheriting from ObservableCollection ). You do the required changes to the collection and then raise the CollectionChanged event with NotifyCollectionChangedAction.Reset . This will instruct the ItemsControl to update itself entirely.

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