Xaml 分层模板格式问题/问题

Xaml 分层模板格式问题/问题,xaml,treeview,hierarchicaldatatemplate,Xaml,Treeview,Hierarchicaldatatemplate,我有几个关于树状视图的分层模板格式的问题。此图将说明: 我想删除字体顶部和边框之间的额外空间 我想将图标居中放置在类型的两行之间 我想加一个逗号。我已经尝试过了,但是在绑定数据中添加逗号时出现了问题 下面是第三级的XAML代码: <HierarchicalDataTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" ItemsSource="{Binding XPath

我有几个关于树状视图的分层模板格式的问题。此图将说明:

  • 我想删除字体顶部和边框之间的额外空间
  • 我想将图标居中放置在类型的两行之间
  • 我想加一个逗号。我已经尝试过了,但是在绑定数据中添加逗号时出现了问题
下面是第三级的XAML代码:

<HierarchicalDataTemplate 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    ItemsSource="{Binding XPath=Unit}"
    >
    <Grid Height="42" Width="auto" >
        <Grid Height="41" HorizontalAlignment="Left" Margin="0,0,0,0" Name="grid1" VerticalAlignment="Top" Width="auto">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto" />
                <ColumnDefinition Width="auto" />
                <ColumnDefinition Width="auto" />
                <ColumnDefinition Width="100" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
            </Grid.RowDefinitions>
            <Image Source= "{Binding XPath=UnitIcon}" Grid.Column="1" Grid.RowSpan="2" VerticalAlignment="Center"  HorizontalAlignment="Left" Stretch="None" OpacityMask="White"></Image>
            <Label Content="{Binding XPath=UnitName}" Height="54" HorizontalAlignment="Left" Name="label4" VerticalAlignment="Top" FontFamily="Smythe" FontSize="18"  Margin="0,0,0,0"  Grid.RowSpan="3" Grid.Column="2" Grid.ColumnSpan="3"/>
            <Label Content="Strength:"  FontFamily="Amaltea WF" FontSize="12" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Center"  Grid.Column="2" Grid.Row="2"/>
            <TextBlock  Text="{Binding XPath=UnitStrength, ConverterParameter=N0}" Margin="0,0,0,0" FontFamily="BauderieScriptSSK Bold" FontSize="18"   HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="3" Grid.Row="2"/>

        </Grid>
         <Line X1='0'
              X2='200'
              Y1='0'
              Y2='0'
              Stroke="Gray"
              StrokeThickness='1' />
    </Grid>
</HierarchicalDataTemplate>

提前感谢您的帮助

  • 从UnitName标签上去掉固定高度。你有网格单元,你不需要固定的高度。部分间隙可能是字体的行距。在标签上临时设置
    Background=“LightSkyBlue”
    ,以查看标签本身实际占用的空间

  • 看起来图像上的
    VerticalAlignment=“Center”
    没有达到预期效果,因为您在所有内容上都设置了冲突的固定高度。您的
    grid1
    固定为41个单位高,但其中的单位名称为54个单位高。布局引擎正在尽最大努力遵守您给出的相互矛盾的命令

    删除XAML中的每个固定高度。每一个人,没有例外。让事情自行决定。如果你绝对要在控件上加上一个固定的高度,考虑把它的内容放在<代码> VIEWBOX 中,这样内容可以在不溢出容器的情况下动态地调整大小。或否;这看起来很奇怪。但首先让你的相对布局工作,然后开始把它塞进任何有限的空间

    当您在XAML布局方面遇到问题时,天真的想法是添加内容。最糟糕的是,添加随机的东西--“我不知道这个属性意味着什么或者它的值意味着什么,但是如果我在这个控件上添加它,它可能会修复另一个的错误!”--充其量,你添加的东西是无害的

    不要那样做删除内容,然后重新构建一次添加一件事,看看它能做什么。并且在没有先阅读文档的情况下不添加任何内容。从Intellisense添加六个随机属性似乎比在MSDN上查找一个属性花费的时间要少,但事实证明并非如此,因为第一种方法总是保证完全浪费时间。这就像驾驶时闭上眼睛,试图通过碰撞障碍物的感觉来驾驶

  • 您将正确的格式字符串分配给了错误的属性。试试这个:

    <TextBlock Text="{Binding XPath=UnitStrength, StringFormat=N0}"
    
    我怀疑他们失败了,因为你给他们的是字符串而不是整数,但这只是猜测

    但这是可行的

    public class IntToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int n = 0;
    
            if (Int32.TryParse((string)value, out n))
            {
                value = n.ToString((String)parameter);
            }
            return value;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    
    XAML
    YOUR_NAMESPACE_此处
    是定义
    inttoString转换程序
    类的C#命名空间。这不一定就在那里;它可以位于父标记或此XAML文件中的任何包含标记上,包括根标记(
    Window
    UserControl
    ResourceDictionary
    )。将其放在这里可以使示例更加独立

    <HierarchicalDataTemplate
        xmlns:local="clr-namespace:YOUR_NAMESPACE_HERE"
        >
        <HierarchicalDataTemplate.Resources>
            <local:IntToStringConverter
                x:Key="IntToString"
                />
        </HierarchicalDataTemplate.Resources>
    
        <!-- blah blah -->
    
        <TextBlock 
            Text="{Binding XPath=UnitStrength, Converter={StaticResource IntToString}, ConverterParameter=N0}" 
            />
    
    HQTemplate
    将是树视图的
    ItemTemplate

    <TreeView 
        ...
        ItemTemplate="{StaticResource HQTemplate}"
    

    我会尝试你的建议,但我想评论一下#3不起作用。在HierarchycalDataTemplate中使用StringFormat有点奇怪。当我测试它时,它非常适合我。唯一的区别可能是您的绑定使用XPath,而我的使用路径(或者绑定到我的属性是int,而您的可能是字符串)。我将连接一个XML源代码,看看我看到了什么。“在HierarchycalDataTemplate中使用StringFormat有点奇怪”--你能详细说明一下你是如何确定HierarchycalDataTemplate是罪魁祸首的吗,你认为它是通过什么机制改变绑定的行为的?我记得几个月前在使用HierarchycalDataTemplate时遇到了这个问题,当时我刚刚放弃了它。但是,正如您所说,我认为问题在于XPath。它只是忽略了任何类型的字符串格式。请尝试以下操作:
    。这是一个猜测(我还在谷歌搜索如何向&*^$%$^&^
    XmlDataProvider
    )提供一个内联XML文档),但我会给出相当大的可能性。
    <Window.Resources>
        <!-- stuff -->
    
        <HierarchicalDataTemplate 
            x:Key="UnitTemplate"
            ItemsSource="{Binding XPath=Unit}"
            >
            <Grid Width="auto">
                <!-- stuff -->
            </Grid>
        </HierarchicalDataTemplate>
    
        <!-- stuff -->
    </Window.Resources>
    
        <TreeView
            ...
            ItemTemplate="{StaticResource UnitTemplate}"
            ...
            />
    
    <TreeView
        ...
        >
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate 
                ItemsSource="{Binding XPath=Unit}"
                >
                <Grid Width="auto">
                    <!-- stuff -->
                </Grid>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>
    
    <Window.Resources>
        <!-- If you're doing the merged thing, you have to explicitly have the 
             ResourceDictionary tag here. 
        -->
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="DataTemplates.xaml" />
            </ResourceDictionary.MergedDictionaries>
    
            <!-- other resources maybe -->
        </ResourceDictionary>
    </Window.Resources>
    
    <ResourceDictionary 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ScenarioEditor"
        >
        <local:IntToStringConverter
            x:Key="IntToString"
            />
    
        <HierarchicalDataTemplate 
            x:Key="UnitTemplate"
            ItemsSource="{Binding XPath=Unit}"
            >
            <Grid Width="auto">
                <!-- stuff -->
            </Grid>
        </HierarchicalDataTemplate>
    
        <HierarchicalDataTemplate 
            x:Key="SomeOtherTemplate"
            >
            <Grid Width="auto">
                <!-- different stuff -->
            </Grid>
        </HierarchicalDataTemplate>
    </ResourceDictionary>        
    
    <HierarchicalDataTemplate
        x:Key="UnitTemplate"
        >
        <Grid>
            <!-- Unit stuff -->
        </Grid>
    </HierarchicalDataTemplate>
    
    <HierarchicalDataTemplate 
        x:Key="CommanderTemplate"
        ItemTemplate="{StaticResource UnitTemplate}"
        >
        <Grid>
            <!-- Commander stuff -->
        </Grid>
    </HierarchicalDataTemplate>
    
    <HierarchicalDataTemplate 
        x:Key="HQTemplate"
        ItemTemplate="{StaticResource CommanderTemplate}"
        >
        <Grid>
            <!-- HQ stuff -->
        </Grid>
    </HierarchicalDataTemplate>
    
    <TreeView 
        ...
        ItemTemplate="{StaticResource HQTemplate}"