更改WPF TreeView的数据类型ItemsSource
我想在点击按钮期间将TreeView
的绑定的数据类型更改为另一种类型。更改WPF TreeView的数据类型ItemsSource
这是TreeView
的代码声明。我想将角色类型从StudentRoles
更改为BursarRoles
。例如:
<local:StudentRoles x:Key="MyData" />
到
<local:BursarRoles x:Key="MyData" />
TreeView
定义:
<UserControl.Resources>
<local:StudentRoles x:Key="MyData" />
<DataTemplate x:Key="LevelFour">
<Border CornerRadius="5" >
<StackPanel Opacity=" 3">
<TextBlock Text="{Binding Path=Name}"
Margin="5 5"
FontFamily="{StaticResource LatoRegular}"
FontSize="{StaticResource FontSizeMedium}"
Foreground="{StaticResource ForegroundVeryDarkBrush}"
/>
</StackPanel>
</Border>
</DataTemplate>
<HierarchicalDataTemplate x:Key="LevelThree"
ItemsSource="{Binding SubRoles}"
ItemTemplate="{StaticResource LevelFour}"
>
<StackPanel Margin="0 3">
<TextBlock Text="{Binding Path=Name}"
Margin="2"
Foreground="{StaticResource ForegroundVeryDarkBrush}"
FontFamily="{StaticResource LatoRegular}"
FontSize="{StaticResource FontSizeMedium}"
/>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="LevelTwo"
ItemsSource="{Binding Roles}"
ItemTemplate="{StaticResource LevelThree}"
>
<StackPanel Margin="0 3">
<TextBlock Text="{Binding Path=RoleDescription}"
Margin="2"
Foreground="{StaticResource ForegroundVeryDarkBrush}"
FontFamily="{StaticResource LatoRegular}"
FontSize="{StaticResource FontSizeLarge}"
/>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="LevelOne"
ItemsSource="{Binding UserRoles}"
ItemTemplate="{StaticResource LevelTwo}"
>
<StackPanel Margin="0 3">
<TextBlock Text="{Binding Path=UserDescription}"
Margin="2"
Foreground="{StaticResource WordOrangeBrush}"
FontFamily="{StaticResource LatoRegular}"
FontSize="{StaticResource FontSizeXLarge}"
/>
</StackPanel>
</HierarchicalDataTemplate>
</UserControl.Resources>
<Grid x:Name="CanScrolGrid">
<TreeView ItemTemplate="{StaticResource LevelOne}"
ItemsSource="{StaticResource MyData}"
Background="{StaticResource ForegroundLightBrush}"
/>
<Button Margin="10" HorizontalAlignment="Center" Height="40" Width="170"
Background="{StaticResource WordBlueBrush}"
Content="Change Roles" Click="Button_Click" />
</Grid>
的UserControl
套控制角色的列表的DataContext
,如StudentRoles
在这背后的代码案例:
public partial class RoleControls : UserControl
{
public RoleControls()
{
InitializeComponent();
this.DataContext = new StudentRoles();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
}
}
角色单独工作正常,但我希望能够在运行时将数据上下文更改为不同的角色。但我不能做很容易,因为这行:
<local:StudentRoles x:Key="MyData" />
我怎么能替换一条线,将允许更改DataContext
这条线?
下面是角色的样本列表(StudentRoles
):
/// <summary>
/// The roles a student can perform on the system
/// </summary>
public class StudentRoles : BaseRole
{
#region Constructor
/// <summary>
/// Default Constructor
/// </summary>
public StudentRoles()
{
RoleType roleType;
Role role;
Role subRole;
UserType userRoles = new UserType("Student");
#region Student Management
roleType = new RoleType("Stock Management");
// Register product
role = new Role("Register Product");
// Single
subRole = new Role("Single");
role.SubRoles.Add(subRole);
// Multiple
subRole = new Role("Multiple");
role.SubRoles.Add(subRole);
roleType.Roles.Add(role);
// Update product Status
role = new Role("Update Product Details");
// Single
subRole = new Role("Single");
role.SubRoles.Add(subRole);
// Multiple
subRole = new Role("Multiple");
role.SubRoles.Add(subRole);
roleType.Roles.Add(role);
// View Product
role = new Role("View Product Details");
// Single
subRole = new Role("Single");
role.SubRoles.Add(subRole);
// by Brand
subRole = new Role("By Brand");
role.SubRoles.Add(subRole);
// bY level
subRole = new Role("By Price");
role.SubRoles.Add(subRole);
// by business Year
subRole = new Role("By Business Year");
role.SubRoles.Add(subRole);
// Dismiss product
role = new Role("Delete Product");
// Single
subRole = new Role("Single");
role.SubRoles.Add(subRole);
// Multiple
subRole = new Role("Multiple");
role.SubRoles.Add(subRole);
// Add the role the list of role type
roleType.Roles.Add(role);
userRoles.UserRoles.Add(roleType);
this.Add(userRoles);
#endregion
}
#endregion
}
根据您发布的代码,看来你是设置DataContext
但当时从来没有真正利用它。
如果没有一个好的Minimal, Complete, and Verifiable code example,不可能知道什么可以工作或在您的情况下是最好的。但是,从目前为止共享的内容看来,您所关注的行应该被删除。即不要声明StudentRoles
或BursarRoles
对象,并根据您当前的状态和用户输入适当设置DataContext
(即,按照您现在正在执行的操作,或按按钮上的BursarRoles
对象)。
当然,在这种情况下需要设置不同的ItemsSource
。如果DataContext
包含要在TreeView
元素中使用的StudentRoles
或BursarRoles
对象,则可以使用ItemsSource="{Binding}"
。例如: -
<TreeView ItemTemplate="{StaticResource LevelOne}"
ItemsSource="{Binding}"
Background="{StaticResource ForegroundLightBrush}"/>
更妙的是,为您打造的视图的*视图模型,与可以根据需要设置一个BaseRole
属性。然后,ItemsSource
将被初始化,例如, ItemsSource="{Binding CurrentRoles}"
,其中CurrentRoles
是根据需要设置的*视图模型中的属性。
这样做的方式,那么顶层视图模型甚至可以有一个具有处理该按钮命令对象(即绑定到该按钮的Command
属性),其中,所述命令目标方法改变CurrentRoles
特性的ICommand
属性。