Ayda Sayed
Ayda Sayed

Reputation: 509

Show Treeview only on item select from combobox

I have a WPF application/MVVM Pattern and it has a combobox and a treeview control. What I wanted to do is to show the treeview control only when i select an item from the combobox.

For example : I have a Property called SelectedTransactionName

      private string _selectedTransactionWsName;
        public string SelectedTransactionName
        {
            set
            {
                if (_selectedTransactionWsName == value) return;
                this._selectedTransactionWsName = value;

                InitializaMessageElement();
            }
            get
            {
                return this._selectedTransactionWsName;
            }

        }

my InitializaMessageElement method will show matching transaction name to the selected item. But now I don't want to show the treeview on page load only when i do a select on the combobox. On page load I want my window to show only the combobox.

Thanks

Upvotes: 0

Views: 346

Answers (1)

devdigital
devdigital

Reputation: 34349

Your view model can contain a calculated boolean property to which your TreeView binds its Visibility property, e.g:

public bool IsTransactionNameSelected
{
    get
    {
        return !string.IsNullOrEmpty(_selectedTransactionWsName);
    }
}

You could then notify property change in the setter of the SelectedTransactionName:

set
{
   if (_selectedTransactionWsName == value) return;
   this._selectedTransactionWsName = value;
   InitializaMessageElement();

   this.NotifyOfPropertyChanged(() => this.IsTransactionNameSelected);
}

Then you can bind your TreeView Visibility property using the supplied BooleanToVisibilityConverter:

<TreeView 
    Visibility="{Binding IsTransactionNameSelected, 
                 Converter={StaticResource BooleanToVisibilityConverter}" ...

Upvotes: 1

Related Questions