Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带切换按钮的TreeView MasterDetails_C#_Wpf_Xaml_Templates_Styles - Fatal编程技术网

C# 带切换按钮的TreeView MasterDetails

C# 带切换按钮的TreeView MasterDetails,c#,wpf,xaml,templates,styles,C#,Wpf,Xaml,Templates,Styles,我想创建一个带有样式的MasterDetails树视图 从那时起,我想重复使用切换按钮 这是我的XAML <Window x:Class="TreeViewMasterDetails.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

我想创建一个带有样式的MasterDetails树视图

从那时起,我想重复使用切换按钮

这是我的XAML

<Window x:Class="TreeViewMasterDetails.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TreeViewMasterDetails" 
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <ControlTemplate x:Key="toggleButtonTemplate" TargetType="ToggleButton">
                <Grid Width="15" Height="13" Background="Transparent">
                    <Path x:Name="ExpandPath"
              HorizontalAlignment="Left" 
              VerticalAlignment="Center" 
              Margin="1,1,1,1"
              Fill="{StaticResource GlyphBrush}"
              Data="M 4 0 L 8 4 L 4 8 Z"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked"
                 Value="True">
                        <Setter Property="Data"
                  TargetName="ExpandPath"
                  Value="M 0 4 L 8 4 L 4 8 Z"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
            <Style x:Key="toggleButtonStyle" TargetType="ToggleButton">
                <Setter Property="Template" Value="{StaticResource toggleButtonTemplate}" />
            </Style>


            <ControlTemplate TargetType="{x:Type TreeViewItem}" x:Key="selectedItemTemplate">

                <Grid Height="Auto">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition Height="Auto"></RowDefinition>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="20"></ColumnDefinition>
                        <ColumnDefinition Width="Auto"></ColumnDefinition>
                    </Grid.ColumnDefinitions>

                    <ToggleButton x:Name="toggleButton" Height="20" Width="20" Style="{StaticResource toggleButtonStyle}" />
                    <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Id}"></TextBlock>
                    <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Name}"></TextBlock>
                    <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Description}"></TextBlock>
                </Grid>
            </ControlTemplate>
        </Grid.Resources>

        <TreeView Height="Auto" 
                  HorizontalAlignment="Stretch" 
                  Margin="10" 
                  VerticalAlignment="Stretch" 
                  Width="Auto"
                  ItemsSource="{Binding Items}">
            <TreeView.ItemContainerStyle>
                <Style TargetType="TreeViewItem">
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Template" Value="{StaticResource selectedItemTemplate}"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TreeView.ItemContainerStyle>
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate DataType="x:Type local:NodeViewModel" ItemsSource="{Binding Children}">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="20*" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="100*" />
                            <ColumnDefinition Width="100*" />
                            <ColumnDefinition Width="100*" />
                        </Grid.ColumnDefinitions>

                        <TextBlock Grid.Column="0" Text="{Binding Id}"></TextBlock>
                        <TextBlock Grid.Column="1" Text="----"></TextBlock>
                        <TextBlock Grid.Column="2" Text="{Binding Name}"></TextBlock>
                    </Grid>

                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </Grid>
</Window>

请原谅XAML的大阻塞,但我真的不知道异常是从哪里来的

但是现在我得到了一个StaticResourceHolder异常,没有更多信息。 我只知道这和切换按钮有关

因此,有两个问题:

  • 如何进一步了解XAML的问题

  • XAML有什么问题

  • 以下是我的项目视图模型:

    public class NodeViewModel : ViewModelBase
        {
            public string Id { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
    
            public ObservableCollection<NodeViewModel> Children { get; set; }
        }
    
    public类nodevewmodel:ViewModelBase
    {
    公共字符串Id{get;set;}
    公共字符串名称{get;set;}
    公共字符串说明{get;set;}
    公共可观测集合子项{get;set;}
    }
    
    查看内部异常。它会告诉你更多的细节。我认为问题就在这里。
    {StaticResource GlyphBrush}
    @nakiya:你是对的,是GlyphBrush,非常感谢!但它的行为仍不像默认项(单击时展开,但显示详细信息)。可能有点复杂。也许你想提供你的评论作为回答,因为它帮助了我,我会接受它。