0

I have a dictionary as the itemsource of a combobox, the displayed value is the key.

public Dictionary<string, ObservableCollection<ViewItem>> LoadedTemplateViews;

In XAML, I have a combobox and a treeview:

<GroupBox Header="Template" Grid.Column="1" Grid.Row="0" Grid.RowSpan="5" Margin="10,10,10,10">
        <DockPanel>
            <ComboBox x:Name="templateCombobox" DockPanel.Dock="Top" Margin="5,10,5,10" ItemsSource="{Binding LoadedTemplateViews, UpdateSourceTrigger=Explicit}" DisplayMemberPath="Key" SelectedValuePath="Key" SelectedValue="default"  />
            <TreeView Name="templateTreeview"   
           ItemsSource="{Binding ElementName=templateCombobox, Path=SelectedItem.Value}"                            
           FontSize="14"   
           AllowDrop="True" Margin="5,5,5,10">
                <i:Interaction.Behaviors>
                    <local:DragDropBehavior/>
                </i:Interaction.Behaviors>
                <TreeView.ItemContainerStyle>
                    <Style TargetType="{x:Type TreeViewItem}">
                        <Setter Property="IsExpanded" Value="True"/>
                    </Style>
                </TreeView.ItemContainerStyle>
                <TreeView.Resources>
                    <HierarchicalDataTemplate DataType="{x:Type model:ViewItem}" ItemsSource="{Binding Path=ViewItems}">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Path=ItemName}" Margin="0,0,10,0"/>
                            <Button Name="DeleteTreeViewItem" Background="Transparent" BorderBrush="Transparent" Click="DeleteTreeViewItem_Click" Visibility="{Binding Path=IsVisible,Converter={StaticResource IntConverter}}">
                                <Image Source="delete_cross.png" Height="15"/>
                            </Button>
                        </StackPanel>
                    </HierarchicalDataTemplate>
                </TreeView.Resources>
            </TreeView>
        </DockPanel>
    </GroupBox>

The DragDropBehavior enables user to drag item into the treeview and each treeview item has a button. By clicking the button, the item will be deleted from the treeview.

The itemsource of the treeview is bound to the combobox. Once the user selects in the combobox, the corresponding value in the dictionary will be displayed in the treeview. Also user can add or delete items from the treeview. After some operations in the treeview, I want to get the existing values from the treeview.

How can I now bind the treeview values to a property in the viewmodel?

I am thinking to use a new property to bind the SelectedItem in treeview, but I got the error saying SelectedItem has now accessible setter.

private ViewItem _selectedTreeViewItem;

public ViewItem SelectedTreeViewItem
{
    get { return _selectedTreeViewItem; }
    set
    {
        if (_selectedTreeViewItem != value)
        {
            _selectedTreeViewItem = value;
            OnPropertyChanged(nameof(SelectedTreeViewItem));
        }
    }
}

Can someone give me a hint? Thank you.

BabyHai
  • 89
  • 1
  • 9
  • You could use a behaviour to bind to a read-only property as suggested in [this blog post](https://blog.magnusmontin.net/2014/01/30/wpf-using-behaviours-to-bind-to-readonly-properties-in-mvvm/). – mm8 Aug 15 '23 at 16:47

0 Answers0