I defined a ListView like this:
<ListView x:Name="libraryBooksListView"
AutomationProperties.AutomationId="VideoListView"
AutomationProperties.Name="Videos"
TabIndex="1"
Padding="0,0,4,0"
ItemsSource="{Binding}"
IsSwipeEnabled="False"
ItemTemplate="{StaticResource LibraryBooksItemTemplate}"
ItemContainerStyle="{StaticResource LibraryListViewItemStyle}"
Grid.Column="1"
SelectionMode="None">
</ListView>
and I defined the LibraryBooksItemTemplate like this:
<DataTemplate x:Key="LibraryBooksItemTemplate">
<Grid Margin="0">
<GridView x:Name="booksGridView"
AutomationProperties.AutomationId="ItemGridView"
AutomationProperties.Name="Grouped Items"
ItemsSource="{Binding Items}"
ItemTemplateSelector="{StaticResource textbookTemplateSelector}"
SelectionMode="Multiple"
IsItemClickEnabled="True"
ItemClick="booksGridView_ItemClick"
SelectionChanged="booksGridView_SelectionChanged"
IsSwipeEnabled="false"
ScrollViewer.VerticalScrollBarVisibility="Auto">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
</Grid>
</DataTemplate>
The GridView, booksGridView, has multiple items (books).
How to modify/access the "ItemTemplateSelector" and SelectionMode etc. of the GridView?
Is there a way to access each of the items in the booksGridView?
Thx
There are many ways to do that.
The easiest one is to wrap your DataTemplate contents into a UserControl.
Another one is to use something like ContainerFromIndex().
Then you can also use VisualTreeHelper class to walk the visual tree.
Then again you can subclass your ItemsControl and override GetContainerForItemOverride() or
PrepareContainerForItemOverride() or
use the ItemContainerGenerator property
The imporant thing to note is that your ItemsSource provides items to the control while the overrides or the generator provide containers to display the items using the ItemTemplates.
*Edit As for your additional questions:
How to modify/access the "ItemTemplateSelector" and SelectionMode etc. of the GridView?
You have defined your selector resource and gave it a key of "textbookTemplateSelector", so you can just get it with this.Resources["textbookTemplateSelector"]. SelectionMode you can bind to the same source DataContext you bound your ItemsSource to and change or read it through a binding.
Is there a way to access each of the items in the booksGridView?
Yes. Since your DataContext is set as the ItemsSource of your ListView - you can access all the items through that DataContext. Each of these items seems to have an Items property that is bound to your GridView, so you can access each of these through the Items property you have defined yourself.
You can access it by using ItemsSource:
foreach(var book in this.booksGridView.ItemsSource)
{
}
Related
I have an ItemsControl with ListViews inside to make a custom grid like layout
Here is roughly what the code looks like to do this, names changed and irrelevant styling is removed.
Each ListView is created by an instance of ItemsSubList and therefore there is no set amount of listboxes or properties to be made.
Does anyone know How I would be able to bind the SelectedItem property of each listbox out somehow, into a list preferably.
<ItemsControl ItemsSource="{Binding Items}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ListView ItemsSource="{Binding ItemsSubList}"
DisplayMemberPath="ItemVersion"
SelectionMode="Single"
Width="100"
VerticalAlignment="Top"
Background="#282828"
BorderBrush="#282828">
</ListView>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I would solve it in your Model.
Your datacontext seems to be a class with a property Items of type ObservableCollection<MyItem> and your class MyItem then as a property ItemsSubList of type ObservableCollection<MySubItem>. Is that right?
The datacontext of your ListView is the instance of type MyItem. You can add a Property SelectedSubItem to your class MyItem and bind it with Two-way-binding to your listbox.SelectedItem.
And then in your main class, that holds your model and where the property Items lives, there I would add a property
public List<MySubItem> SelectedSubItems => Items.Select(i => i.SelectedSubItem).ToList();, a property that dynamically selects all selected subitems from all your items.
I've a ObservableCollection, when I want to display this ObservableCollection with CheckBox I am simply binding the contents to CheckBox but when I want to display the same collection by using ComboBox I am unable to do that. Any suggestions?
XAML: Display collection using CheckBox --WORKS
<ItemsControl ItemsSource="{Binding Synonyms}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" Margin="0,5,0,0" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<!--Display items in CheckBox-->
<CheckBox Content="{Binding Display}" Margin="10,0,0,0" /> // Display is the collection.
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Output:
XAML: Display collection using ComboBox --NOTHING OVER HERE
<ItemsControl ItemsSource="{Binding Synonyms}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" Margin="0,5,0,0" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<!--Display items in ComboBox.-->
Approach - 1
<ComboBox>
<ComboBoxItem Content="{Binding Display}"/>
</ComboBox>
Approach - 2
<ComboBox ItemsSource="{Binding Path=Synonyms}" DisplayMemberPath="Display"/>
Approach - 3
<ComboBox >
<ComboBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Green" BorderThickness="1" Padding="5">
<TextBlock Text="{Binding Path=Display,StringFormat='Display: {0}'}" />
</Border>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Output:
I want to display items(one,two,three, etc.) inside oneComboBox with Select All option. I've tried several approaches but nothing. What am I missing here?
The checkbox is not designed to hold multiple items unlike the Combobox so implementing a system designed for the checkbox is not relevant for the combobox.
To solve your issue, remove the combobox from the ItemsControl and have it stand on its own:
<ComboBox ItemsSource="{Binding Synonyms}" DisplayMemberPath="Display"/>
Which tells the combobox to bind its ItemsSource to the data context which is unspecified, which is fine, so it then gets it's parent's data context. That process works its way up to each parent until it finds a bound data context (most likely the page's datacontext to a VM instance).
Assuming that the data context is valid at some point in the visual tree, it will bind to that instance and look for a property named Synonyms. From the Synonyms property it will use that as a list to display items.
To show (display) text in the combobox (instead defaulting to the item's ToString()) the combobox will show the string from the item's property Display.
Giving a list of items in one drop down.
The short answer, is you should use a ComboBox as the root element, not ItemsControl. CompboBox is just a specialized version of ItemsControl.
<ComboBox ItemsSource="{Binding Synonyms}" DisplayMemberPath="Display"/>
The longer answer.
ComboBox derives from ItemsControl, so you get all the features the base class, plus additional features.
ItemsControl (and its derived classes) provides a way of repeating a set of data in the UI. The DataTemplate is where you specify what UI you want for each "row" of data in the Synonyms source.
What you are doing is asking Silverlight to create a separate ComboBox for each underlying data row.
You can still use a DataTemplate within the ComboBox. Like this.
<ComboBox ItemsSource="{Binding Synonyms}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<!--Display items in CheckBox-->
<TextBox Text="{Binding Display}"
Margin="10,0,0,0"
FontWeight="Bold" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I have two ListView, each bound to a specific collection in my ViewModel and I want to be able to select only one item globally.
Each ListView has its SelectedItem property bound to the same property in the ViewModel.
My probleme is as follow: when I select an item in a ListView, and then select another item in the other ListView, the first item stays selected.
I could achieve this with some code-behind, but I want to know if a pure XAML solution exists.
XAML:
<ListView SelectionMode="Single" ItemsSource="{Binding Path=MyList1}" SelectedItem="{Binding Path=MySelectedItem}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ListView SelectionMode="Single" ItemsSource="{Binding Path=MyList2}" SelectedItem="{Binding Path=MySelectedItem}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Title}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I finally got it working, by just replacing SelectedItem by SelectedValue.
In this use case, both properties have the same behaviour, but the latter one handles correctly the unselection if the bound selected item is not in the list.
You need to use Mode=TwoWay in your SelectedItem. It should work.
In my C# windows phone app, I create a binding to bind a list of string to ItemsControl.
// MyCollections is a List<string>
<ItemsControl x:Name="ContentRoot" ItemsSource="{Binding MyCollections}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding }" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
It works. But My question is how can I add my own item (e.g. 'Click to add more') to this ItemsControl after it is binded?
There are two answers to this question:
Use an ObservableCollection instead of a List, as it will notify the UI when items are added/removed from it. Then you just add your new item to the list in the view model.
Use a CompositeCollection so you can have the "additional" item without modifying the actual collection.
Normally you would do 1, but since you want a "Click to add more" type of option, CompositeCollection is probably the way to go.
Since you metioned windows phone (but tagged WPF) you may want to look at this post for how to write your own CompositeCollection object: how to do a CompositeCollection in WP8?
Use CompositeCollection to add additional items in your XAML. This should work:
<StackPanel x:Name="stackPanel">
<StackPanel.Resources>
<CompositeCollection x:Key="myCollection">
<CollectionContainer Collection="{Binding DataContext.MyCollections,
Source={x:Reference stackPanel}}"/>
<ContentControl Content="Click to add more"/>
</CompositeCollection>
</StackPanel.Resources>
<ItemsControl x:Name="ContentRoot"
ItemsSource="{StaticResource myCollection}"/>
</StackPanel>
I have the following binding on my ListView, which works fine:
<ListView ItemsSource="{Binding Filters}" Margin="5" Name="FiltersList" Height="100"/>
Filters is actually a type of List<LogFilter>. LogFilters have a property on them called Title. I would like to show the Title of each element in the Filters list in my ListView instead. Is this possible? Something like ItemsSource="{Binding Filters.Title}"?
Just set the ItemTemplate to display the title:
<ListView ItemsSource="{Binding Filters}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Title}"></TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
It seems to me that you don't need to use ListView at all. For your needs ListBox with DisplayMemberPath would be enough:
<ListBox ItemsSource="{Binding Filters}" DisplayMemberPath="Title"/>