Add user controls to Dockpane at runtime

219
5
05-05-2020 06:23 AM
ModyBuchbinder
Esri Regular Contributor

Hi all

I have a user control that gives some options for a layer (include layer name and some  comboboxes depend of the specific layer).

When I press a button to open the Dockpane I would like to insert this user control once for each layer in the map and fill them up.

If I have 3 layers I will insert 3 user controls into the Dockpane and if I have 5 layers - 5 user controls.

No need to get the event of adding layers, I assume the user will press the button if he change the number of layers.

Any code samples?

Thanks

0 Kudos
5 Replies
GintautasKmieliauskas
Regular Contributor

Hi Mody,

I would use DataGrid in your case. I have similar situation with my workspace properties. Each property has simple value or value you can choose from combobox. Each combobox has different sets of available values. What you need is a list of custom objects with layer and other properties inside. Other things will do bindings.

You can use DataGridComboBoxColumn for columns for comboboxes or make your own template for column.

My implementation of DataGrid:

0 Kudos
ModyBuchbinder
Esri Regular Contributor

Hi Gintautas

I made a few tests but I am not able to fill each combobox in a different line with different values.

The values is determine by my program and not taking from any table.

Can you put a few lines of code?

Thanks

0 Kudos
GintautasKmieliauskas
Regular Contributor

Hi Mody,

You need to use different templates in xaml for each combobox column. The xaml source of my previous picture is very difficult . It uses triggers and etc.. I have tried to simplify xaml but I am not really sure about bindings validity.

        <DataGrid Style="{DynamicResource Esri_DataGrid}" HeadersVisibility="Column"
                  IsReadOnly="False" ItemsSource="{Binding AvailableParameters, Mode=TwoWay}" SelectedIndex="{Binding SelectedParameterIndex}" SelectionUnit="FullRow" 
                  ScrollViewer.CanContentScroll="True" VerticalScrollBarVisibility="Auto">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Name, Mode=OneWay}" IsReadOnly="True" Width="1*"/>
                <DataGridComboBoxColumn Header="Value" ItemsSource="{Binding DataContext.StringValues, RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}}"
                                        SelectedItemBinding="{Binding DataContext.ComboValue, RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}, Mode=TwoWay}" Width="1*">
                </DataGridComboBoxColumn>
                <DataGridComboBoxColumn Header="Value2" ItemsSource="{Binding DataContext.StringValues2, RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}}"
                                        SelectedItemBinding="{Binding DataContext.ComboValue2, RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}, Mode=TwoWay}" Width="1*">
                </DataGridComboBoxColumn>
            </DataGrid.Columns>
        </DataGrid>
Your binding list object class must have properties StringValues and ComboValue which return some data depending on your layer type. If you need two combox columns  then you need to add additional properties StringValues2 and  ComboValue2 for next column.
Another way is binding to window datacontext:
Both ways are a little bit  tricky, but you can find more info on internet.
0 Kudos
ModyBuchbinder
Esri Regular Contributor

I got this part but the question is how do you do DataGrid.Items.Add in runtime adding different combobox values to each row.

0 Kudos
GintautasKmieliauskas
Regular Contributor

You need to use ObservableCollection for datagrid binding and object of your collection must implement INotifyPropertyChanged. I implement IEditableObject too.

Then you create new item you must to fill StringValues and ComboValue properties too depending on your layer object. 

0 Kudos