首页 > 解决方案 > WPF - 如何更改 ContentControl 单击 TreeViewItem 的内容?

问题描述

我正在开发一个 WPF 项目,但我遇到了一个问题。我已经搜索了 2 天的解决方案,但从未找到任何可以帮助我的东西......我有一个内部包含不同数据类型的 TreeView 和一个显示与这些不同数据类型相对应的视图的 ContentControl。我希望,当我单击 TreeViewItem 时,根据此 TreeViewItem 中包含的数据类型,它会更改 ContentControl 中的视图。我根据所选的 TreeviewItem 执行不同的命令,但它永远不会改变视图......有人有答案或想法可以帮助我吗?

我将它用于我的 TreeView :

<TreeView x:Name="networkTree" ScrollViewer.VerticalScrollBarVisibility="Auto" MaxHeight="490" TreeViewItem.Selected="GetHeaderSelectedItem"/>

它执行这个:

public class NetworkConfigViewModel : BindableBase
{
    private IRegionManager regionManager;
    private bool _showRDConf;
    private bool _showNetworkConf;
    private bool _showDeviceInfo;

    public NetworkConfigViewModel(IRegionManager regionManager)
    {
        this.regionManager = regionManager;
        regionManager.RegisterViewWithRegion("NetworkConfigInfoRegion", typeof(NetworkConfigInfoView));
        regionManager.RegisterViewWithRegion("RDConfigurationRegion", typeof(RDConfigurationView));
        regionManager.RegisterViewWithRegion("DeviceInfoRegion", typeof(DeviceInfoView));
        ShowNetworkConfCommand = new DelegateCommand(NetworkConf);
        ShowRDConfCommand = new DelegateCommand(RDConf);
        ShowDeviceInfoCommand = new DelegateCommand(DevInfo);
        _showNetworkConf = true;
        _showRDConf = false;
        _showDeviceInfo = false;
    }

    public bool ShowRDConf
    {
        get
        {
            return _showRDConf;
        }
        set
        {
            SetProperty(ref _showRDConf, value);
        }
    }

    public bool ShowNetworkConf
    {
        get
        {
            return _showNetworkConf;
        }
        set
        {
            SetProperty(ref _showNetworkConf, value);
        }
    }

    public bool ShowDeviceInfo
    {
        get
        {
            return _showDeviceInfo;
        }
        set
        {
            SetProperty(ref _showDeviceInfo, value);
        }
    }

    public DelegateCommand ShowNetworkConfCommand { get; set; }

    public DelegateCommand ShowRDConfCommand { get; set; }

    public DelegateCommand ShowDeviceInfoCommand { get; set; }

    private void NetworkConf()
    {
        ShowRDConf = false;
        ShowDeviceInfo = false;
        ShowNetworkConf = true;
        System.Windows.Forms.MessageBox.Show("Commande ShowNetConf executée :\nShowNetworkConf="+ShowNetworkConf.ToString()+"\nShowRDConf="+ShowRDConf.ToString() + "\nShowDeviceInfo=" + ShowDeviceInfo.ToString());
    }

    private void RDConf()
    {
        ShowNetworkConf = false;
        ShowDeviceInfo = false;
        ShowRDConf = true;
        System.Windows.Forms.MessageBox.Show("Commande ShowRConf executée :\nShowRDConf="+ShowRDConf.ToString()+"\nShowNetworkConf="+ShowNetworkConf.ToString() + "\nShowRDeviceInfo=" + ShowDeviceInfo.ToString());
    }

    private void DevInfo()
    {
        ShowNetworkConf = false;
        ShowRDConf = false;
        ShowDeviceInfo = true;
        System.Windows.Forms.MessageBox.Show("Commande ShowDeviceInfo executée :\nShowDeviceInfo=" + ShowDeviceInfo.ToString() + "\nShowNetworkConf=" + ShowNetworkConf.ToString() + "\nShowRDConf=" + ShowRDConf.ToString());
    }

    public void ChangeNetworkView(TreeView tree)
    {
        TreeViewItem tvi = null;
        tvi = tree.SelectedItem as TreeViewItem;
        if (tvi != null)
        {
            if (tvi.Tag.ToString() == "Network")
            {
                ShowNetworkConfCommand.Execute();
            }
            else if(tvi.Tag.ToString()=="RD")
            {
                ShowRDConfCommand.Execute();
            }
            else if (tvi.Tag.ToString() == "VD")
            {
                ShowDeviceInfoCommand.Execute();
            }
        }
    }
}

对于我的 ContentControl :

        <ContentControl x:Name="RDView" x:FieldModifier="public" Grid.Column="0" Grid.Row="1" Grid.RowSpan="2" Grid.ColumnSpan="2" Visibility="{Binding Path=ShowRDConf, Converter={StaticResource Convert}}" prism:RegionManager.RegionName="RDConfigurationRegion"/>
        <ContentControl x:Name="NetworkView" x:FieldModifier="public" Grid.Column="0" Grid.Row="1" Grid.RowSpan="2" Grid.ColumnSpan="2" Visibility="{Binding Path=ShowNetworkConf, Converter={StaticResource Convert}}" prism:RegionManager.RegionName="NetworkConfigInfoRegion"/>
        <ContentControl x:Name="DeviceView" x:FieldModifier="public" Grid.Column="0" Grid.Row="1" Grid.RowSpan="2" Grid.ColumnSpan="2" Visibility="{Binding Path=ShowDeviceInfo, Converter={StaticResource Convert}}" prism:RegionManager.RegionName="DeviceInfoRegionq"/>

提前谢谢你

标签: wpfxamltreeviewcontentcontroltreeviewitem

解决方案


推荐阅读