


This article mainly introduces the WPF collection control to implement the separator ItemsControl Separator in detail. It has a certain reference value. Interested friends can refer to it.
is often needed in WPF collection controls. Insert a separator style between each collection item, but WPF's ItemsControl does not have direct implementation of related functions, so we can only consider curves to save the country. After research, I have probably thought of the following two implementation methods.
First write the data template of ItemsControl, as follows:
##
<ItemsControl ItemsSource="{Binding Source}" BorderThickness="1" BorderBrush="Blue" VerticalAlignment="Stretch"> <ItemsControl.ItemTemplate> <DataTemplate> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Border Name="Bd" Grid.Row="0" Height="1" Background="Red" /> <TextBlock Grid.Row="1" Text="{Binding}" /> </Grid> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>The Border named Bd It is the separator. At this time, the separator can be seen in the header of each item. Now our goal is to hide the separator of the first item. This achieves the purpose of having separators between items.
The firstimplementation method is the simplest, using the collection item to forward bind PreviousData. This is one of the four binding methods, and it is probably the one that is used the least. , but it comes in handy at this time. The code is as follows:
<DataTemplate.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource PreviousData}}" Value="{x:Null}"> <Setter TargetName="Bd" Property="Visibility" Value="Collapsed" /> </DataTrigger> </DataTemplate.Triggers>When the previous item of a certain item is empty, the separator is hidden. It can be done with a simple line of code. However, one disadvantage of this implementation is that if you use the Insert method to add data to the front of the bound data source, more than one item without a separator will appear. If you add data to the end of the queue or to the middle of the queue, there will be no This problem occurs.
The second implementation method is to use ItemsControl#AlternationCount and AlternationIndex attribute to mark the index number for the collection item, and then hide the separator of the item with index number 0. The code is as follows:
Copy code The code is as follows :
First bind AlternationCount to the Count property of the data source on
ItemsControl, and then the AlternationIndex property of ItemsControl becomes the index number of the collection data source. , just write the logic in the trigger:
<Border Name="Bd" Grid.Row="0" Height="1" Background="Red"> <Border.Style> <Style TargetType="{x:Type Border}"> <Style.Triggers> <DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}" Value="0"> <Setter Property="Visibility" Value="Collapsed" /> </DataTrigger> </Style.Triggers> </Style> </Border.Style> </Border>The trigger determines that when the index number is 0, it will be hidden
Border, the amount of code in this method is not large, and the advantage is that it can definitely realize this function, whether inserting to the beginning or the end of the queue, but AlternationCount and The original meaning of the AlternationIndex attribute is to implement functions such as interlaced color changing. This function is occupied at this time, so if your collection wants to implement both separator and alternate line style functions, you may need to add an additional converter. , but the content of the converter is also very simple. You can restore the previous function by finding the remainder.
The above is the detailed content of WPF collection control implements separator (ItemsControl Separator). For more information, please follow other related articles on the PHP Chinese website!

C#.NET is a powerful development platform that combines the advantages of the C# language and .NET framework. 1) It is widely used in enterprise applications, web development, game development and mobile application development. 2) C# code is compiled into an intermediate language and is executed by the .NET runtime environment, supporting garbage collection, type safety and LINQ queries. 3) Examples of usage include basic console output and advanced LINQ queries. 4) Common errors such as empty references and type conversion errors can be solved through debuggers and logging. 5) Performance optimization suggestions include asynchronous programming and optimization of LINQ queries. 6) Despite the competition, C#.NET maintains its important position through continuous innovation.

The future trends of C#.NET are mainly focused on three aspects: cloud computing, microservices, AI and machine learning integration, and cross-platform development. 1) Cloud computing and microservices: C#.NET optimizes cloud environment performance through the Azure platform and supports the construction of an efficient microservice architecture. 2) Integration of AI and machine learning: With the help of the ML.NET library, C# developers can embed machine learning models in their applications to promote the development of intelligent applications. 3) Cross-platform development: Through .NETCore and .NET5, C# applications can run on Windows, Linux and macOS, expanding the deployment scope.

The latest developments and best practices in C#.NET development include: 1. Asynchronous programming improves application responsiveness, and simplifies non-blocking code using async and await keywords; 2. LINQ provides powerful query functions, efficiently manipulating data through delayed execution and expression trees; 3. Performance optimization suggestions include using asynchronous programming, optimizing LINQ queries, rationally managing memory, improving code readability and maintenance, and writing unit tests.

How to build applications using .NET? Building applications using .NET can be achieved through the following steps: 1) Understand the basics of .NET, including C# language and cross-platform development support; 2) Learn core concepts such as components and working principles of the .NET ecosystem; 3) Master basic and advanced usage, from simple console applications to complex WebAPIs and database operations; 4) Be familiar with common errors and debugging techniques, such as configuration and database connection issues; 5) Application performance optimization and best practices, such as asynchronous programming and caching.

C# is widely used in enterprise-level applications, game development, mobile applications and web development. 1) In enterprise-level applications, C# is often used for ASP.NETCore to develop WebAPI. 2) In game development, C# is combined with the Unity engine to realize role control and other functions. 3) C# supports polymorphism and asynchronous programming to improve code flexibility and application performance.

C# and .NET are suitable for web, desktop and mobile development. 1) In web development, ASP.NETCore supports cross-platform development. 2) Desktop development uses WPF and WinForms, which are suitable for different needs. 3) Mobile development realizes cross-platform applications through Xamarin.

The C#.NET ecosystem provides rich frameworks and libraries to help developers build applications efficiently. 1.ASP.NETCore is used to build high-performance web applications, 2.EntityFrameworkCore is used for database operations. By understanding the use and best practices of these tools, developers can improve the quality and performance of their applications.

How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

WebStorm Mac version
Useful JavaScript development tools
