Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a ListView, where each list view item will contain a Label and a Button. On clicking on the button, the respective ListView Item should be selected.

I have created a Data Template to keep the Label and Button for the List view items.

How can I achieve this, I tried by using ICollectionView. But I was not able to achieve the required functionality.

XML
<Window.Resources>
<DataTemplate x:Key="dt2">
            <StackPanel Orientation="Horizontal">
                <Label Content="{Binding Path=FirstName}"/>
                <Button Content="Show" Command="{Binding RelativeSource = {RelativeSource    FindAncestor, AncestorType={x:Type ListView}}, Path=DataContext.ShowCustomer}"/>
            </StackPanel>
        </DataTemplate>
</Window.Resources>

<ListView ItemsSource="{Binding Path=Customers}" ItemTemplate="{StaticResource ResourceKey=dt2}" SelectedItem="{Binding CurrentCustomerToBeShown}" IsSynchronizedWithCurrentItem="True"/>
Posted
Comments
Alexander Dymshyts 25-Oct-13 3:54am    
You need on button click show new listview with customer info, right?

Try this..


XML
<ListView ItemsSource="{Binding Path=List}" ItemTemplate="{StaticResource ResourceKey=dt2}" SelectedItem="{Binding CurrentCustomerToBeShown}" IsSynchronizedWithCurrentItem="True">
            <ListView.Resources>
                <Style TargetType="ListViewItem">
                    <Style.Triggers>
                        <Trigger Property="IsKeyboardFocusWithin" Value="True">
                            <Setter Property="IsSelected" Value="True"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ListView.Resources>
        </ListView>
 
Share this answer
 
HI Mahesh thanks for the solution, every thing is working fine but it not working for the first item in the ListView. The first is not getting selected on clicking button.

How can I apply the same for a TabControl ?

C#
<TabControl ItemsSource="{Binding CurrentlyShownCustomers}" SelectedItem="{Binding CurrentCustomer}" Grid.Column="1">
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <ListView ItemsSource="{Binding CurrentlyShownCustomers}">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding FirstName}"/>
                            <Button Height="20" Width="20" Content="X" Grid.Column="1" Command="{Binding RelativeSource={RelativeSource FindAncestor, 
                                AncestorType={x:Type TabControl}}, Path=DataContext.CloseTabItem}"/>
                        </StackPanel>
                    </ListView>
                </DataTemplate>
</TabControl>
 
Share this answer
 
v3
Hello

If you don't mind do some code-behind, this does the job too:
C#
lb.PreviewMouseLeftButtonDown += (MouseButtonEventHandler)(
    (o, e) => {
        ListViewItem lbItem = UITools.FindAncestor<listviewitem>(e.OriginalSource as DependencyObject) as ListViewItem;
        bool isListViewItem = lbItem != null;
        bool isButtonClicked = UITools.FindAncestor<button>(e.OriginalSource as DependencyObject) is Button;
        if (isListViewItem )
        {
            if (isButtonClicked)
                lbItem.SetValue(ListBoxItem.IsSelectedProperty, true);
            else e.Handled = true; //Handled means will not pass to ListView for selection.
        }                    
    });

FindAncenstor method: from UITools.cs[^]
C#
public static T FindAncestor<t>(DependencyObject obj) where T : DependencyObject
{
    while (obj != null)
    {
        T o = obj as T;
        if (o != null)
            return o;
        obj = VisualTreeHelper.GetParent(obj);
    }
    return default(T);
}</t>


Regards
Joseph Leung
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900