How to get selected ListBoxItem(s)?

665
6
03-14-2022 02:45 AM
TomGeo
by
Occasional Contributor III

I publish a list of objects in a ListBox and hoped to bind the selection of an item to a boolean property.

 

<ListBox x:Name="DataLayerList" SelectionMode="Extended"    IsTextSearchEnabled="True" ItemsSource="{Binding LayersFiltered, Mode=OneWay}">
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="IsSelected" Value="{Binding ListItemSelected, Mode=TwoWay}"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

 

 

LayersFiltered is a list of ThemeLayer, where Theme layer is defined as such.

 

public class ThemeLayer
    {
        public readonly string Name;
        public readonly string Id;
        public readonly string Type;
        
        public override string ToString()
        {
            return Name;
        }

        public ThemeLayer(string name, string id, string type)
        {
            Name = name;
            Id = id;
            Type = type;
        }

        public bool IsSelected { get; set; }
    }

 

 

Within my ViewModel LayersFiltered and ListItemSelected are defined as:

 

private List<ThemeLayer> _layersFiltered;
public List<ThemeLayer> LayersFiltered
    {
        get => _layersFiltered;
        set => SetProperty(ref _layersFiltered, value, () => LayersFiltered);
    }

private bool _listItemSelected;
public bool ListItemSelected
    {
        get => _listItemSelected;
        set 
        {
            SetProperty(ref _listItemSelected, value, () => ListItemSelected);
        }
    }

 

 

However, ListItemSelected is never triggered when I select a ListBoxItem.

 

I had a look at https://github.com/Esri/arcgis-pro-sdk-community-samples/blob/44183d0d5a9bc5da6fb8b9229211cc707dd2b8... but fail to transfer it into my code.

 

 

- We are living in the 21st century.
GIS moved on and nobody needs a format consisting out of at least three files! No, nobody needs shapefiles, not even for the sake of an exchange format. Folks, use GeoPackage to exchange data with other GIS!
0 Kudos
6 Replies
TomGeo
by
Occasional Contributor III

Thanks, but I am not interested in a Taco Bell survey 😄 Chance you pasted the wrong link in?

- We are living in the 21st century.
GIS moved on and nobody needs a format consisting out of at least three files! No, nobody needs shapefiles, not even for the sake of an exchange format. Folks, use GeoPackage to exchange data with other GIS!
0 Kudos
DanPatterson
MVP Esteemed Contributor

I removed it to Spam


... sort of retired...
GKmieliauskas
Esri Regular Contributor

Hi, 

I think the problem is with one way binding of LayersFiltered.

Try to set two ways as in sample:

ItemsSource="{Binding LayersFiltered, Mode=TwoWay}

 

0 Kudos
TomGeo
by
Occasional Contributor III

I tried TwoWay before and it did not change the behaviour... Would be still interested in a solution for TwoWay binding. 🙂

I solved it now with OneWay binding. In need of a button to react on the users selection I implemented the button by referencing the ListBox and its SelectedItems property.

<Button Content="Add selected data" Grid.Row="3"
                VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="0,5"
                Style="{DynamicResource Esri_Button}" Command="{Binding AddServiceCommand}" 
                CommandParameter="{Binding ElementName=DataLayerList, Path=SelectedItems}"/>

 

That works without any problem and the custom command to execute the buttons functionality receives a collection of objects of type ThemeLayer.

- We are living in the 21st century.
GIS moved on and nobody needs a format consisting out of at least three files! No, nobody needs shapefiles, not even for the sake of an exchange format. Folks, use GeoPackage to exchange data with other GIS!
0 Kudos
GKmieliauskas
Esri Regular Contributor

Two way binding is in RemoveAddins sample

 

 

            <ListBox Height="185"
                ScrollViewer.VerticalScrollBarVisibility="Auto"                
                ScrollViewer.HorizontalScrollBarVisibility="Disabled"                  
                ItemsSource="{Binding AddIns, Mode=TwoWay}"
                ItemTemplate="{StaticResource AddInItem}"
                SelectionMode="Multiple">
                <ListBox.ItemContainerStyle>
                    <!-- This Style binds a ListBoxItem to a the ViewModelItem. -->
                    <Style TargetType="ListBoxItem">
                        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                    </Style>
                </ListBox.ItemContainerStyle>
            </ListBox>

 

 

Try to check all other settings of listbox. One of them could be ItemTemplate="{StaticResource AddInItem}". They use template for listbox item. You can do the same.

0 Kudos
TomGeo
by
Occasional Contributor III

I tried several times to get this part with the ItemTemplate and the namespace right, and failed every time. What usually happened was that the project ran, but my ListBox was not showing any content anymore.

- We are living in the 21st century.
GIS moved on and nobody needs a format consisting out of at least three files! No, nobody needs shapefiles, not even for the sake of an exchange format. Folks, use GeoPackage to exchange data with other GIS!
0 Kudos