I'm totally new to wpf and silverlight, and I have A LOT to learn...
I've got a listbox which contain a template
<ListBox ItemsSource="{Binding itemList}" x:Name="list">
<ListBox.ItemTemplate>
<DataTemplate x:Name="datatemplate" >
<Grid Name="{Binding Id}">
<TextBlock Text="{Binding Txt}"></TextBlock>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
i need to assign a AutomationProperties.AutomationId to each list item, like in
<ListBoxItem x:Name="lb_a" AutomationProperties.AutomationId="lb_1">
<Grid Name="grid_a">
<TextBlock Text="aa"></TextBlock>
</Grid>
</ListBoxItem>
<ListBoxItem x:Name="lb_b" AutomationProperties.AutomationId="lb_2">
<Grid Name="grid_b">
<TextBlock Text="bb"></TextBlock>
</Grid>
</ListBoxItem>
...
how can i do?
is that even possible?
You can set your attached property in ItemContainerStyle:
<ListBox ItemsSource="{Binding itemList}" x:Name="list">
<ListBox.ItemTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="AutomationProperties.AutomationId" Value="{Binding MyAutomationIdProperty}"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
Related
I would like to search items in my ListBox by typing. I found it can be done by TextSearch Property.
ListBox XAML:
<ListBox x:Name="SzablonyBox" HorizontalAlignment="Center"
HorizontalContentAlignment="Center" VerticalAlignment="Center"
MinWidth="100" MinHeight="100" BorderBrush="{x:Null}"
Background="{x:Null}" TextSearch.TextPath="Text" IsTextSearchEnabled="True">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<EventSetter Event="PreviewKeyDown" Handler="ListBoxItem_PreviewKeyDown"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel>
<TextBlock Text="{Binding nazwa}" FontWeight="Bold"
FontSize="15" Height="35"
VerticalAlignment="Center" HorizontalAlignment="Center"
TextAlignment="Justify"/>
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
How to set TextSearch.TextPath="Text" to look after ListBox items (TextBlock text) ?
I have some problem with ComboBox nested in ListBox. I want to add the same ItemsSource(gained from database, adding from codebehind) to each of comboboxes created in ListBox, but don't know how. Any ideas how to do this?
</Window.Resources>
<Style x:Key="lbxKey" TargetType="ListBox">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate DataType="{x:Type Type1}">
<StackPanel Orientation="Horizontal" Width="Auto" HorizontalAlignment="Stretch">
<TextBlock TextTrimming="CharacterEllipsis" Width="200" Text="{Binding NAMETYPE1}" HorizontalAlignment="Left"></TextBlock>
<ComboBox HorizontalAlignment="Stretch" Tag="{Binding IDTYPE1}">
<ComboBox.ItemsSource>
<!-- no idea what should be here or even if this is needed -->
</ComboBox.ItemsSource>
<ComboBox.ItemTemplate>
<DataTemplate DataType="{x:Type Type2}">
<StackPanel Orientation="Horizontal" Width="100">
<TextBlock Text="{Binding NAMETYPE2}" TextTrimming="CharacterEllipsis"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
In codebehind:
lbxListbox.ItemsSource = observableCollectionFromDatabase;
//here should be sth to set ItemsSource for comboboxes in ListBox
There should be a collection type property in your item class (anything that implements IEnumerable will do). Then you would bind the ComboBox's ItemSource like this:
<ComboBox Tag="{Binding IDTYPE1}" ItemsSource="{Binding ITEMS}" ...>
I have a List View
<ListView ItemsSource="{Binding Apparatus.AcidBaseApparatus}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Tag="{Binding AppratusName}">
<TextBlock Text="{Binding AppratusName}" Background="Azure">
</TextBlock>
<Image HorizontalAlignment="Center" MaxHeight="50" Source="{Binding ImageSource}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
<i:Interaction.Behaviors>
<behave:ApparatusDragBehavior></behave:ApparatusDragBehavior>
</i:Interaction.Behaviors>
</ListView>
I want to Bind the tag of each ListViewItem to ApparatusName as I have done with the StackPanel in the Data Template. I couldn't find any option fiddling with template on my own. Is it possible to do so?
You need to set the Binding in ItemContainerStyle (targeting ListViewItem):
<ListView ItemsSource="{Binding Apparatus.AcidBaseApparatus}">
<!-- ... -->
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Tag" Value="{Binding AppratusName}"/>
</Style>
</ListView.ItemContainerStyle>
<!-- ... -->
</ListView>
I am binding a PivotItems but somehow the items are not resized properly, according to my margin declaration. Why is that?
EDIT: here is my new code and it still has gaps..
<Pivot x:Name="TpsSegmentsPivot" Title="Locator" Foreground="#FF888888" Style="{StaticResource PivotStyle1}" SelectionChanged="Pivot_SelectionChanged" Margin="0" Grid.Row="1" ItemTemplate="{StaticResource TpTemplate}" ItemsSource="{Binding DataSource}">
<Pivot.HeaderTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding id}" Margin="0, 16, 0, 0" Foreground="#FF888888" FontSize="32" FontFamily="Segoe WP" FontWeight="Light"/>
</Grid>
</DataTemplate>
</Pivot.HeaderTemplate>
</Pivot>
This is the template:
<DataTemplate x:Key="TpTemplate">
<ListBox Background="Black"
ItemsSource="{Binding Seg}"
ItemTemplate="{StaticResource SectionUCTemplate}"
HorizontalAlignment="Stretch"
VerticalAlignment="Top">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</DataTemplate>
Here is a screenshot with the red pivotItem..
I think the issue with the extra space is the Piviot Item you are using inside the DataTemplate tag. remove it and bind the header to a header template. I've used data binding on Pivots before and never seen this issue.
From one of my released apps:
<controls:Pivot Title="CAMPING CHECKLIST" ItemsSource="{Binding FilteredCategories}" Name="MainPivot">
<controls:Pivot.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</controls:Pivot.HeaderTemplate>
<controls:Pivot.ItemTemplate>
<DataTemplate>
<!-- Your pivot item content here -->
</DataTemplate>
</controls:Pivot.ItemTemplate>
</controls:Pivot>
I believe the PivotItem in a PivotItem is the root of your issue.
your code should look similar to:
<controls:Pivot ItemsSource="{Binding tripTypeViewModel.TripTypeViewModelDataSource}" x:Name="TripsSegmentsPivot" Title=" " Foreground="#FF888888" Style="{StaticResource PivotStyle1}" SelectionChanged="Pivot_SelectionChanged" Grid.Row="1">
<controls:Pivot.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding DataContext.tripTypeViewModel.HeaderText, ElementName=TripsSegmentsPivot}" Margin="0, 16, 0, 0" Foreground="#FF888888" FontSize="32" FontFamily="Segoe WP" FontWeight="Light"/>
</DataTemplate>
</controls:Pivot.HeaderTemplate>
<controls:Pivot.ItemTemplate>
<DataTemplate>
<ListBox x:Name="ItemsLB" ItemsSource="{Binding DataContext.tripTypeViewModel.Segment, ElementName=TripsSegmentsPivot}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Transparent">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<local:SectionUC HorizontalAlignment="Stretch" Grid.Row="1" VerticalAlignment="Top"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
</controls:Pivot.ItemTemplate>
</controls:Pivot>
The first image is how I describe it, the second is how you currently have it :
This is the standard behaviour for PivotItems since they are always displayed with a margin. You should set a negative margin:
margin="-20,0,-20,0"
I have nested ListBoxes:
<ListBox Name="listbox" Padding="0,0,0,100" Loaded="listbox_Loaded" Foreground="Black">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Name}" FontSize="30" FontWeight="Bold"/>
<ListBox ItemsSource="{Binding Categories}" Foreground="Black">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
When I touch and drag items from inner listbox it plays scroll animation for this inner listbox. How to prevent this behavior? I need to scroll the outer listbox only, but the items from inner listbox still must be selectable.
Thank you!
Try to change Template of inner ListBox to be only ItemsPresenter. This will remove ScrollViewer which is normally part of that template:
<ListBox ItemsSource="{Binding Categories}" Foreground="Black">
<ListBox.Template>
<ControlTemplate TargetType="ListBox">
<ItemsPresenter/>
</ControlTemplate>
</ListBox.Template>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>