2017-10-28 199 views
0

我想在點擊按鈕期間將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 
} 

回答

0

根據您發佈的代碼,看來你是設置DataContext但當時從來沒有真正利用它。

如果沒有一個好的Minimal, Complete, and Verifiable code example,不可能知道什麼可以工作或在您的情況下是最好的。但是,從目前爲止共享的內容看來,您所關注的行應該被刪除。即不要聲明StudentRolesBursarRoles對象,並根據您當前的狀態和用戶輸入適當設置DataContext(即,按照您現在正在執行的操作,或按按鈕上的BursarRoles對象)。

當然,在這種情況下需要設置不同的ItemsSource。如果DataContext包含要在TreeView元素中使用的StudentRolesBursarRoles對象,則可以使用ItemsSource="{Binding}"。例如: -

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

更妙的是,爲您打造的視圖的頂級視圖模型,與可以根據需要設置一個BaseRole屬性。然後,ItemsSource將被初始化,例如, ItemsSource="{Binding CurrentRoles}",其中CurrentRoles是根據需要設置的頂級視圖模型中的屬性。

這樣做的方式,那麼頂層視圖模型甚至可以有一個具有處理該按鈕命令對象(即綁定到該按鈕的Command屬性),其中,所述命令目標方法改變CurrentRoles特性的ICommand屬性。