Binding cells in ListView
-
Is there a way i could bind each cell in the ListViewItem to a different Property? <Window x:Class="SDKSample.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Window.Resources> <!--Define data for ListView--> <XmlDataProvider x:Key="MyData" XPath="/Info"> <x:XData> <Info xmlns=""> <AccessEvents Name="A" /> <AccessEvents Name="B" /> <AccessEvents Name="C"/> </Info> </x:XData> </XmlDataProvider> <Style x:Key="MyContainer" TargetType="{x:Type ListViewItem}"> <Setter Property="Margin" Value="0,1,0,0"/> <Setter Property="Height" Value="21"/> <Style.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="Foreground" Value="Blue" /> <Setter Property="Cursor" Value="Hand"/> </Trigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsSelected" Value="true" /> <Condition Property="Selector.IsSelectionActive" Value="true" /> </MultiTrigger.Conditions> <Setter Property="Foreground" Value="Yellow" /> </MultiTrigger> </Style.Triggers> </Style> <DataTemplate x:Key="SecondCell"> <StackPanel Orientation="Horizontal"> <CheckBox IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}"/> </StackPanel> </DataTemplate> <!--</SnippetCheckBoxDataTemplate>--> </Window.Resources> <StackPanel> <TextBlock Foreground="Blue" FontSize="14" Margin="10,10,0,0"> Select Song to Play </TextBlock> <!--<SnippetListView>--> <ListView ItemsSource="{Binding Source={StaticResource MyData}, XPath=AccessEvents}" ItemContainerStyle="{StaticResource MyContainer}" SelectionChanged="mySelectionChanged" SelectionMode="Single" Name="myPlaylist" Margin="10,10,0,0"> <ListView.View> <GridView> <!--<SnippetGridViewColumnCheckBox>-->
-
Is there a way i could bind each cell in the ListViewItem to a different Property? <Window x:Class="SDKSample.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Window.Resources> <!--Define data for ListView--> <XmlDataProvider x:Key="MyData" XPath="/Info"> <x:XData> <Info xmlns=""> <AccessEvents Name="A" /> <AccessEvents Name="B" /> <AccessEvents Name="C"/> </Info> </x:XData> </XmlDataProvider> <Style x:Key="MyContainer" TargetType="{x:Type ListViewItem}"> <Setter Property="Margin" Value="0,1,0,0"/> <Setter Property="Height" Value="21"/> <Style.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="Foreground" Value="Blue" /> <Setter Property="Cursor" Value="Hand"/> </Trigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsSelected" Value="true" /> <Condition Property="Selector.IsSelectionActive" Value="true" /> </MultiTrigger.Conditions> <Setter Property="Foreground" Value="Yellow" /> </MultiTrigger> </Style.Triggers> </Style> <DataTemplate x:Key="SecondCell"> <StackPanel Orientation="Horizontal"> <CheckBox IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}"/> </StackPanel> </DataTemplate> <!--</SnippetCheckBoxDataTemplate>--> </Window.Resources> <StackPanel> <TextBlock Foreground="Blue" FontSize="14" Margin="10,10,0,0"> Select Song to Play </TextBlock> <!--<SnippetListView>--> <ListView ItemsSource="{Binding Source={StaticResource MyData}, XPath=AccessEvents}" ItemContainerStyle="{StaticResource MyContainer}" SelectionChanged="mySelectionChanged" SelectionMode="Single" Name="myPlaylist" Margin="10,10,0,0"> <ListView.View> <GridView> <!--<SnippetGridViewColumnCheckBox>-->
First you need to think if you really need to do this. It seems a bit odd what you want. One way to do it, is using a template selector (DataTemplateSelector to be more precise). With this thing you can create several templates and, based on some properties of the object you are binding to (in this case "Name"), select one of them. Of course this only works if you only have a few templates... Here's a nice example: http://www.beacosta.com/blog/?p=16[^] Mihai,
-
First you need to think if you really need to do this. It seems a bit odd what you want. One way to do it, is using a template selector (DataTemplateSelector to be more precise). With this thing you can create several templates and, based on some properties of the object you are binding to (in this case "Name"), select one of them. Of course this only works if you only have a few templates... Here's a nice example: http://www.beacosta.com/blog/?p=16[^] Mihai,
-
Yes I would go for ItemDataTemplate, this is what I need I have a list of strings and a list of checkbox like this A, Checkbox Hello B, CheckBox Hi C, CheckBox Bye How do i show it in a ListView? Thanks In Advance
Like this:
<Window x:Class="SDKSample.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Window.Resources> <XmlDataProvider x:Key="MyData" XPath="/Info"> <x:XData> <Info xmlns=""> <AccessEvents Name="A" Text="Hello"/> <AccessEvents Name="B" Text="Hi" /> <AccessEvents Name="C" Text="Bye" /> </Info> </x:XData> </XmlDataProvider> <DataTemplate x:Key="SecondCell"> <CheckBox IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}" Content ="{Binding XPath=@Text}"/> </DataTemplate> </Window.Resources> <ListView ItemsSource="{Binding Source={StaticResource MyData}, XPath=AccessEvents}" > <ListView.View> <GridView> <GridViewColumn Header="Access Event" DisplayMemberBinding="{Binding XPath=@Name}" Width="150"/> <GridViewColumn Header="Deliver" CellTemplate="{StaticResource SecondCell}" Width="80"/> </GridView> </ListView.View> </ListView> </Window>