Sam_vdd
Sam_vdd

Reputation: 696

Multiple Listviews with only one SelectedItem

I have multiple Listviews each binded with their own itemsource. But i only have 1 Selected Item.

So for example i have 5 listboxes (Monday, Tuesday, ...) each of them with their own itemssource (MondayList, TuesdayList, ...). Each of these Listviews SelectedItem property is binded to The Property 'CurrentToDo'.

The problem is that if i select one in the Monday and then select one in Tuesday they are both selected.

So only one item should be able to be selected throughout all the listviews, please help.

<UserControl.Resources>        
    <Style x:Key="ListViewItemStyleToDo" TargetType="{x:Type ListViewItem}">
        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
    </Style>

    <GridView x:Key="ViewBase1" x:Shared="False" >
        <GridViewColumn Header="" Width="30">
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}" />
                </DataTemplate>
            </GridViewColumn.CellTemplate>
        </GridViewColumn>
        <GridViewColumn Header="Subject" Width="auto" DisplayMemberBinding="{Binding Subject}" />
    </GridView>
</UserControl.Resources>

<ListView Grid.Row="1"  ItemsSource="{Binding MondayList}" SelectedItem="{Binding CurrentToDo, Mode=TwoWay}" SelectionMode="Single" ItemContainerStyle="{DynamicResource ListViewItemStyleToDo}" View="{DynamicResource ViewBase1}" />

<ListView Grid.Row="3"  ItemsSource="{Binding TuesdayList}" SelectedItem="{Binding CurrentToDo,Mode=TwoWay}" SelectionMode="Single" ItemContainerStyle="{DynamicResource ListViewItemStyleToDo}" View="{DynamicResource ViewBase1}"  />

Property:

    private ToDoMod _currentToDo;

    public ToDoMod CurrentToDo
    {
        get { return _currentToDo; }
        set
        {
            _currentToDo= value;
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("CurrentToDo"));
        }
    }

Upvotes: 6

Views: 1998

Answers (2)

XAMeLi
XAMeLi

Reputation: 6289

You'd need to add 5 more properties: MondayCurrentToDo, TuesdayCurrentToDo...

Bind each list view's SelectedItem to the respective property.

In setter of each property, update CurrentToDo and remove selection of other lists - so it will look like this:

private ToDoMod _mondayCurrentToDo;

public ToDoMod MondayCurrentToDo
{
    get { return _mondayCurrentToDo; }
    set
    {
        _mondayCurrentToDo= value;
        CurrentToDo = _mondayCurrentToDo;
        _tuesdayCurrentToDo = null;
        //TODO: add null setters for all other _<dayOfWeek>CurrentToDo members


        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs("MondayCurrentToDo"));
            PropertyChanged(this, new PropertyChangedEventArgs("TuesdayCurrentToDo"));
            //TODO: add notificaitons for all other <DayOfWeek>CurrentToDo properties                
        }
    }
}

Of course, refactor this into methods, etc...

Upvotes: 0

Sam_vdd
Sam_vdd

Reputation: 696

Here is the answer:

Just bind to SelectedValue instead of SelectedItem.

Upvotes: 11

Related Questions