Wpf 仅在从组合框中选择的项目上显示树视图

Wpf 仅在从组合框中选择的项目上显示树视图,wpf,mvvm,Wpf,Mvvm,我有一个WPF应用程序/MVVM模式,它有一个组合框和一个treeview控件。 我想做的是仅当我从组合框中选择一个项目时才显示treeview控件 例如:我有一个名为SelectedTransactionName的属性 private string _selectedTransactionWsName; public string SelectedTransactionName { set {

我有一个WPF应用程序/MVVM模式,它有一个组合框和一个treeview控件。 我想做的是仅当我从组合框中选择一个项目时才显示treeview控件

例如:我有一个名为SelectedTransactionName的属性

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

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

        }
我的InitializaMessageElement方法将显示与所选项目匹配的事务名称。但是现在我不想只在组合框上进行选择时才在页面加载时显示treeview。 在页面加载时,我希望我的窗口只显示组合框


感谢您的视图模型可以包含一个计算布尔属性,您的
TreeView
将其
可见性
属性绑定到该属性,例如:

public bool IsTransactionNameSelected
{
    get
    {
        return !string.IsNullOrEmpty(_selectedTransactionWsName);
    }
}
然后,您可以在
SelectedTransactionName
的setter中通知属性更改:

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

   this.NotifyOfPropertyChanged(() => this.IsTransactionNameSelected);
}
然后,您可以使用提供的
BooleanToVisibilityConverter
绑定您的
TreeView
可见性属性:

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