C# 在Treeview选择更改上切换用户控件
如何根据treeview选择更改切换用户控件。我已经在ListBox上实现了这一点,但不知道如何使用Wpf Treeview实现这一点。这是我的XAML代码C# 在Treeview选择更改上切换用户控件,c#,wpf,xaml,mvvm,treeview,C#,Wpf,Xaml,Mvvm,Treeview,如何根据treeview选择更改切换用户控件。我已经在ListBox上实现了这一点,但不知道如何使用Wpf Treeview实现这一点。这是我的XAML代码 <Window x:Class="MainScreen" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:
<Window x:Class="MainScreen"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewModelSettings="clr-namespace:ViewModel.Settings" >
<Window.Resources>
<DataTemplate DataType="{x:Type viewModelSettings:BasicSettingsViewModel}">
<viewSettings:BasicSettingsView/>
</DataTemplate>
<DataTemplate DataType="{x:Type viewModelSettings:AdvancedSettingsViewModel}">
<viewSettings:AdvancedSettingsView/>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<ListBox x:Name="ListBoxMenu"
Grid.Column="0" Margin="5,5,5,385"
ItemsSource="{Binding Settings}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" Padding="10"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Border Grid.Column="1" Margin="5">
<ContentControl Content="{Binding ElementName=ListBoxMenu, Path=SelectedItem}"/>
</Border>
</Grid>
</Window>
//选项DialogViewModel类
public class OptionsDialogViewModel : ViewModelBase
{
private readonly ObservableCollection<SettingsViewModelBase> _settings;
public ObservableCollection<SettingsViewModelBase> Settings
{
get { return this._settings; }
}
public OptionsDialogViewModel ()
{
_settings = new ObservableCollection<SettingsViewModelBase>();
_settings.Add(new BasicSettingsViewModel());
_settings.Add(new AdvancedSettingsViewModel());
}
}
现在,我的ViewModel是从这个设置ViewModelBase派生的
public class AdvancedSettingsViewModel : SettingsViewModelBase
{
public override string Name
{
get { return "Advanced"; }
}
}
我现在有两个问题,这是完成这项任务的正确方法吗?
如何将列表视图切换到树状视图不幸的是,用树状视图选择项目并不像用列表框选择那样简单
与使用
Content=“{binding ElementName=ListBoxMenu,Path=SelectedItem}”
简单地绑定到列表框不同,绑定到treeview项需要一些代码。查看SO线程,了解更多关于“如何”和“为什么”的讨论。您可以在所选节点上执行此操作。如何执行此操作,您可以发布一些示例代码吗?为什么需要树状视图?您当前的列表框不工作吗?@aherocalledFrog,条目数量太多,大约100个,因此我更喜欢使用树状视图进行逻辑分组
public abstract class SettingsViewModelBase : ViewModelBase
{
public abstract string Name { get; }
}
public class AdvancedSettingsViewModel : SettingsViewModelBase
{
public override string Name
{
get { return "Advanced"; }
}
}