【问题标题】:Switch User control on Treeview Selection Change在 Treeview 选择更改上切换用户控件
【发布时间】:2015-06-08 16:26:22
【问题描述】:

如何根据树形视图选择更改切换用户控件。我已经在 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: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>

我正在使用数据模板来定义各种 Viewmodel 并与它们绑定视图

为了让它完全 MVVM,这是我的代码

 public partial class MainScreen : Window
  {
     public MainScreen()
     {
         InitializeComponent();
         DataContext = new OptionsDialogViewModel();
     }
 }

// OptionsDialogViewModel 类

 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());
    }
}

// SettingsViewModelBase 类

public abstract class SettingsViewModelBase : ViewModelBase
{
    public abstract string Name { get; }
}

现在我的 ViewModel(s) 是从这个 SettingsViewModelBase 派生的

public class AdvancedSettingsViewModel : SettingsViewModelBase
{
    public override string Name
    {
        get { return "Advanced"; }
    }
}

我现在有 2 个问题,这是完成这项任务的正确方法吗? 如何将我的列表视图切换到树视图

【问题讨论】:

  • 你可以在选定的节点上这样做
  • 怎么做,可以发一些示例代码吗?
  • 为什么需要 TreeView?你当前的 ListBox 不工作吗?
  • @aherocallFrog,条目数量太大,100左右,所以我更喜欢树视图做逻辑分组

标签: c# wpf xaml mvvm treeview


【解决方案1】:

不幸的是,使用树形视图选择项目不如使用列表框选择那么简单。

与简单地使用Content="{Binding ElementName=ListBoxMenu, Path=SelectedItem}" 绑定到ListBox 不同,绑定到树视图项涉及一些代码隐藏。查看 SO 主题 herehere 了解更多关于如何和为什么的讨论。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-14
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 2018-10-05
    • 1970-01-01
    • 2017-10-25
    • 2016-09-28
    相关资源
    最近更新 更多