【问题标题】:Change the data type of a WPF TreeView ItemsSource更改 WPF TreeView ItemsSource 的数据类型
【发布时间】:2018-04-09 22:37:59
【问题描述】:

我想在单击按钮期间将TreeView 绑定的数据类型更改为另一种类型。

这是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
}

【问题讨论】:

    标签: c# wpf xaml data-binding treeview


    【解决方案1】:

    根据您发布的代码,您似乎正在设置DataContext,但实际上从未使用过它。

    如果没有一个好的Minimal, Complete, and Verifiable code example,就不可能确定在你的场景中什么会起作用或最好。但是,从您迄今为止分享的内容来看,您所关注的行似乎应该被删除。 IE。不要声明StudentRolesBursarRoles 对象,并根据您当前的状态和用户输入适当地设置DataContext(即您现在正在执行的StudentRoles 对象或BursarRoles点击按钮上的对象)。

    当然,ItemsSource 在这种情况下需要进行不同的设置。如果DataContext 包含要在TreeView 元素中使用的StudentRolesBursarRoles 对象,则可以只使用ItemsSource="{Binding}"。例如:

    <TreeView ItemTemplate="{StaticResource LevelOne}" 
              ItemsSource="{Binding}" 
              Background="{StaticResource ForegroundLightBrush}"/>
    

    更好的做法是为视图创建一个顶级视图模型,并根据需要设置BaseRole 属性。然后,ItemsSource 将从那里初始化,例如ItemsSource="{Binding CurrentRoles}",其中CurrentRoles 是您根据需要设置的顶级视图模型中的属性。

    这样做,那么顶级视图模型甚至可以有一个ICommand 属性,该属性有一个处理按钮的命令对象(即绑定到按钮的Command 属性),其中命令目标方法更改CurrentRoles 属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-17
      • 1970-01-01
      • 1970-01-01
      • 2020-02-27
      • 2014-01-26
      • 2014-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多