330 likes | 461 Vues
This presentation covers advanced techniques for utilizing ListBox controls in XAML, focusing on data binding, styling, and custom templates. Attendees will learn how to dynamically populate ListBoxes with data using ObjectDataProvider and implement custom styles to improve UI aesthetics. The session also includes examples of applying data triggers for interactivity and enhancing user experience through customized control templates. Join us to discover the best practices for managing resources and templates in WPF applications.
E N D
PimpMyListbox Rainer Stropekcubidobusinesssolutionsgmbh
ElementBaum Window StackPanel TextBlock TextBox
Logischer und visueller Baum Border AdornerDecorator ContentPresenter Window AdornerLayer StackPanel TextBlock String TextBox String ... (ScrollViewer, Grid, Rectangle, ScrollBar, etc.)
[ContentPropertyAttribute("Items")] [LocalizabilityAttribute(LocalizationCategory.None, Readability=Readability.Unreadable)] [StyleTypedPropertyAttribute(Property="ItemContainerStyle", StyleTargetType=typeof(FrameworkElement))] public class ItemsControl : Control, IAddChild <ListBox> <TextBlock Text="MyTextBlock" /> <Button Content="My Button" /> <Rectangle Width="30" Height="10" Fill="Gray" /> <sys:String>Ein Text!</sys:String> <sys:Random /> </ListBox>
<Page ... xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:sysio="clr-namespace:System.IO;assembly=mscorlib" Margin="10"> <Page.Resources> <ObjectDataProvider ObjectType="{x:Type sysio:DirectoryInfo}" MethodName="GetFiles" x:Key="Files"> <ObjectDataProvider.ConstructorParameters> <sys:String>[...]\Images</sys:String> </ObjectDataProvider.ConstructorParameters> </ObjectDataProvider> </Page.Resources> <ListBox ItemsSource="{Binding Source={StaticResource Files}}" /> </Page>
Resourcen Lokale Resourcen <ListBox ..."> <ListBox.Resources> <Style TargetType="ListBox"> ... </Style> </ListBox.Resources> </ListBox> Resourcen in Windows oder Pages Resourcen in app.xaml <Page.Resources> <Style TargetType="ListBox"> ... </Style> <Style TargetType="ListBoxItem"> ... </Style> </Page.Resources> <Application.Resources> <Style TargetType="ListBox"> ... </Style> ... </Application.Resources>
Styles <ListBoxGrid.Column="0" Grid.Row="1" ItemsSource="{Binding Source={StaticResource Files}}"> <ListBox.Resources> <Style TargetType="ListBox"> <Setter Property="Background" Value="LightGray" /> <Setter Property="FontSize" Value="11" /> </Style> </ListBox.Resources> </ListBox>
Styling der Listbox Demo...
Data Trigger <DataTemplateDataType="{x:Type sysio:FileInfo}"> ... <DataTemplate.Triggers> <DataTrigger Binding="{Binding Path=IsReadOnly}" Value="True"> <Setter Property="TextBlock.Foreground" Value="Gray" /> <Setter Property="TextBlock.FontStyle" Value="Italic" /> </DataTrigger> </DataTemplate.Triggers> </DataTemplate>
Data Template mit Data Trigger Demo...
Control Template <Style TargetType="Button"> <Setter Property="Template"> <Setter.Value> <ControlTemplateTargetType="Button"> <Grid> <Ellipse Fill="{StaticResourceButtonBrush}" Stroke="DarkGray" /> <ContentPresenterHorizontalAlignment="Center" VerticalAlignment="Center" /> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>
Control Template mit Trigger Demo...
<ListBoxScrollViewer.HorizontalScrollBarVisibility="Disabled"><ListBoxScrollViewer.HorizontalScrollBarVisibility="Disabled"> <ListBox.ItemsPanel> <ItemsPanelTemplate> <WrapPanel /> </ItemsPanelTemplate> </ListBox.ItemsPanel> <TextBlock Text="MyTextBlock" /> <Button Content="My Button" /> <Rectangle Width="30" Height="10" Fill="Gray" /> <sys:String>Ein Text!</sys:String> <sys:Random /> ... </ListBox>
Custom Control Demo...
r.stropek@cubido.at oder rainer@software-architects.at Karin Huber, Rainer Stropek Software Architects