2017-10-28 8 views
0

ボタンクリック中にTreeViewのバインディングのデータ型を別の種類に変更したいとします。WPFのデータ型を変更するTreeView ItemsSource

これはTreeViewのコード宣言です。ロールのタイプをStudentRolesからBursarRolesに変更したいと思います。たとえば、次のように

<local:StudentRoles x:Key="MyData" /> 

TreeView

<local:BursarRoles x:Key="MyData" /> 

の定義:

<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への変更を許可する行で、この行を置き換えることができますか?あなたが投稿したコードに基づいて

/// <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を設定するが、その後は決して実際にそれを利用していることが表示されます:

は、ここでの役割のサンプルリスト(StudentRoles)です。

いいえMinimal, Complete, and Verifiable code exampleがないと、あなたのシナリオで何がうまくいくのか、それとも最善であるのかを知ることは不可能です。しかし、あなたがこれまでに共有してきたことから、心配している行は削除する必要があるようです。私。 StudentRolesまたはBursarRolesオブジェクトを宣言しておらず、現在の状態とユーザーの入力に応じてDataContextを適切に設定してください(StudentRolesオブジェクト、またはボタンクリックのBursarRolesオブジェクト)。

もちろん、ItemsSourceは、その場合に異なって設定する必要があります。 DataContextTreeView要素で使用したいStudentRolesまたはBursarRolesオブジェクトを保持している場合は、ItemsSource="{Binding}"を使用できます。例えば:

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

さらに良いことに、あなたが必要に応じて設定することができBaseRoleプロパティで、ビューのトップレベルのビューモデルを作成するためだろう。次に、ItemsSourceがそれから初期化される。 ItemsSource="{Binding CurrentRoles}"ここで、CurrentRolesは、必要に応じて設定する最上位ビューモデルのプロパティです。そのように完了

、その後、トップレベルのビューモデルも、コマンドターゲットメソッドがCurrentRolesプロパティを変更するボタン(すなわち、ボタンのCommandプロパティにバインド)、処理コマンドオブジェクトを有するICommand性質を有することができます。

関連する問題