HorizontalAlignment =“ Stretch”在TreeViewItem中不起作用

拉丹

这是一个不起作用的示例:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication2"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <TreeView HorizontalAlignment="Stretch">
        <TreeViewItem Header="Stuff" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch">
            <Grid HorizontalAlignment="Stretch">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Label Grid.Column="0">Some random text</Label>
                <TextBox Grid.Column="1"></TextBox>

            </Grid>
        </TreeViewItem>
    </TreeView>
</Window>

我希望文本框可以延伸到窗口的宽度,但是您会得到以下信息:

看起来像什么

运行时看起来像什么

键入时,文本框会扩展,这不是我想要的。

我在想现在可以将第二列的宽度设置为的宽度,TreeViewItem但是我不确定如何做到这一点。

我试过将网格放在各种面板中,但这也不起作用。我还设置HorizontalAlignmentHorizontalContentAlignmentStretch对文本本身,但也不能正常工作。

这是Grid控件的一些限制吗?将文本框单独放在树状视图中将使其按我的意愿扩展,但是我需要在其旁边放置标签,如果有多个字段,并且所有文本框都对齐,则看起来会更好。

更新

使用不同的ControlTemplate标题可以拉伸,但内容仍然不能拉伸。

<TreeView HorizontalAlignment="Stretch">
        <TreeViewItem Style="{DynamicResource StretchableTreeViewItemTemplate}" HorizontalAlignment="Stretch" Header="Stuff">
            <Grid HorizontalAlignment="Stretch">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Label Grid.Column="0">Some random text</Label>
                <TextBox Grid.Column="1" HorizontalAlignment="Stretch"></TextBox>
            </Grid>
        </TreeViewItem>
    </TreeView>



<Style x:Key="StretchableTreeViewItemTemplate" TargetType="TreeViewItem"
   BasedOn="{StaticResource {x:Type TreeViewItem}}">
        <Setter Property="HorizontalContentAlignment"
      Value="Center" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TreeViewItem">
                    <StackPanel>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"
                            MinWidth="19" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition />
                            </Grid.RowDefinitions>
                            <!--
                         Note that the following do not work, but I believe the top 2 should?!
                         <ToggleButton IsChecked="{TemplateBinding IsExpanded}" ClickMode="Press" Name="Expander">
                         <ToggleButton IsChecked="{TemplateBinding Property=IsExpanded}" ClickMode="Press" Name="Expander">
                         <ToggleButton IsChecked="{TemplateBinding Path=IsExpanded}" ClickMode="Press" Name="Expander">
                    -->
                            <ToggleButton IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}}"
                      ClickMode="Press"
                      Name="Expander">
                                <ToggleButton.Style>
                                    <Style TargetType="ToggleButton">
                                        <Setter Property="UIElement.Focusable"
                      Value="false" />
                                        <Setter Property="FrameworkElement.Width"
                      Value="16" />
                                        <Setter Property="FrameworkElement.Height"
                      Value="16" />
                                        <Setter Property="Control.Template">
                                            <Setter.Value>
                                                <ControlTemplate TargetType="ToggleButton">
                                                    <Border Padding="5,5,5,5"
                            Background="#00FFFFFF"
                            Width="16"
                            Height="16">
                                                        <Path Fill="#00FFFFFF"
                            Stroke="#FF989898"
                            Name="ExpandPath">
                                                            <Path.Data>
                                                                <PathGeometry Figures="M0,0L0,6L6,0z" />
                                                            </Path.Data>
                                                            <Path.RenderTransform>
                                                                <RotateTransform Angle="135"
                                           CenterX="3"
                                           CenterY="3" />
                                                            </Path.RenderTransform>
                                                        </Path>
                                                    </Border>
                                                    <ControlTemplate.Triggers>
                                                        <Trigger Property="UIElement.IsMouseOver"
                               Value="True">
                                                            <Setter TargetName="ExpandPath"
                                Property="Shape.Stroke"
                                Value="#FF1BBBFA" />
                                                            <Setter TargetName="ExpandPath"
                                Property="Shape.Fill"
                                Value="#00FFFFFF" />
                                                        </Trigger>
                                                        <Trigger Property="ToggleButton.IsChecked"
                               Value="True">
                                                            <Setter TargetName="ExpandPath"
                                Property="UIElement.RenderTransform">
                                                                <Setter.Value>
                                                                    <RotateTransform Angle="180"
                                             CenterX="3"
                                             CenterY="3" />
                                                                </Setter.Value>
                                                            </Setter>
                                                            <Setter TargetName="ExpandPath"
                                Property="Shape.Fill"
                                Value="#FF595959" />
                                                            <Setter TargetName="ExpandPath"
                                Property="Shape.Stroke"
                                Value="#FF262626" />
                                                        </Trigger>
                                                    </ControlTemplate.Triggers>
                                                </ControlTemplate>
                                            </Setter.Value>
                                        </Setter>
                                    </Style>
                                </ToggleButton.Style>
                            </ToggleButton>
                            <Border x:Name="Bd"
                HorizontalAlignment="Stretch"
                BorderThickness="{TemplateBinding Border.BorderThickness}"
                BorderBrush="{TemplateBinding Border.BorderBrush}"
                Padding="{TemplateBinding Control.Padding}"
                Background="{TemplateBinding Panel.Background}"
                SnapsToDevicePixels="True"
                Grid.Column="1">
                                <ContentPresenter x:Name="PART_Header"
                            Content="{TemplateBinding HeaderedContentControl.Header}"
                            ContentTemplate="{TemplateBinding HeaderedContentControl.HeaderTemplate}"
                            ContentStringFormat="{TemplateBinding HeaderedItemsControl.HeaderStringFormat}"
                            ContentTemplateSelector="{TemplateBinding HeaderedItemsControl.HeaderTemplateSelector}"
                            ContentSource="Header"
                            HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
                            SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                            </Border>
                            <ItemsPresenter x:Name="ItemsHost"
                        Grid.Column="1"
                        Grid.Row="1" />
                        </Grid>
                    </StackPanel>
                    <ControlTemplate.Triggers>
                        <Trigger Property="TreeViewItem.IsExpanded"
               Value="False">
                            <Setter TargetName="ItemsHost"
                Property="UIElement.Visibility"
                Value="Collapsed" />
                        </Trigger>
                        <Trigger Property="ItemsControl.HasItems"
               Value="False">
                            <Setter TargetName="Expander"
                Property="UIElement.Visibility"
                Value="Hidden" />
                        </Trigger>
                        <Trigger Property="TreeViewItem.IsSelected"
               Value="True">
                            <Setter TargetName="Bd"
                Property="Panel.Background"
                Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                            <Setter Property="TextElement.Foreground"
                Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="TreeViewItem.IsSelected"
                     Value="True" />
                                <Condition Property="Selector.IsSelectionActive"
                     Value="False" />
                            </MultiTrigger.Conditions>
                            <Setter TargetName="Bd"
                Property="Panel.Background"
                Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
                            <Setter Property="TextElement.Foreground"
                Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
                        </MultiTrigger>
                        <Trigger Property="UIElement.IsEnabled"
               Value="False">
                            <Setter Property="TextElement.Foreground"
                Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

结果:

编辑了控制模板

尝试使用Pikoh的解决方案:

<local:StretchingTreeView HorizontalAlignment="Stretch">
    <local:StretchingTreeViewItem HorizontalAlignment="Stretch" Header="Stuff">
        <Grid HorizontalAlignment="Stretch">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Label Grid.Column="0">Some random text</Label>
            <TextBox Grid.Column="1" HorizontalAlignment="Stretch"></TextBox>
        </Grid>
    </local:StretchingTreeViewItem>
</local:StretchingTreeView>

Pikoh解决方案的结果

皮科

尝试这样的事情:

<TextBox Grid.Column="1" Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type Grid}}, Mode=OneWay}"></TextBox>

编辑

好的,我在这里找到了一种解决方案它基本上会创建一个继承Treeview的新类:

class StretchingTreeView : TreeView
{
    protected override DependencyObject GetContainerForItemOverride()
    {
        return new StretchingTreeViewItem();
    }

    protected override bool IsItemItsOwnContainerOverride(object item)
    {
        return item is StretchingTreeViewItem;
    }
}

class StretchingTreeViewItem : TreeViewItem
{
    public StretchingTreeViewItem()
    {
        this.Loaded += new RoutedEventHandler(StretchingTreeViewItem_Loaded);
    }

    private void StretchingTreeViewItem_Loaded(object sender, RoutedEventArgs e)
    {
        // The purpose of this code is to stretch the Header Content all the way accross the TreeView. 
        if (this.VisualChildrenCount > 0)
        {
            Grid grid = this.GetVisualChild(0) as Grid;
            if (grid != null && grid.ColumnDefinitions.Count == 3)
            {
                // Remove the middle column which is set to Auto and let it get replaced with the 
                // last column that is set to Star.
                grid.ColumnDefinitions.RemoveAt(1);
            }
        }
    }

    protected override DependencyObject GetContainerForItemOverride()
    {
        return new StretchingTreeViewItem();
    }

    protected override bool IsItemItsOwnContainerOverride(object item)
    {
        return item is StretchingTreeViewItem;
    }
}

用法示例:

<Window x:Class="WpfApplication2.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication2"
    Title="Window2" Height="300" Width="300">
<Grid>

    <local:StretchingTreeView HorizontalAlignment="Stretch" >

        <local:StretchingTreeViewItem Header="Stuff" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" >
            <Grid HorizontalAlignment="Stretch" >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Label Grid.Column="0">Some random text</Label>
                <TextBox Grid.Column="1" ></TextBox>

            </Grid>
        </local:StretchingTreeViewItem>
    </local:StretchingTreeView>
</Grid>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

UPDATE语句在sqlite中不起作用

来自分类Dev

Ajax在Wordpress管理中不起作用

来自分类Dev

ScrollView的SubView中的UIButton不起作用

来自分类Dev

将图例文本的HorizontalAlignment设置为八度的左侧

来自分类Dev

Scalatest在IntelliJ 13.1中不起作用

来自分类Dev

为什么我的flexbox flex-stretch属性不起作用?

来自分类Dev

通用App ListView项目HorizontalAlignment

来自分类Dev

HorizontalChildrenAlignment =“ Stretch”在WrapGrid中不起作用

来自分类Dev

onbeforeunload在Windows Chrome中不起作用

来自分类Dev

重复字符在PowerShell中不起作用

来自分类Dev

XAML HorizontalAlignment属性无法满足我的要求

来自分类Dev

在WPF中显示BitmapSource不起作用

来自分类Dev

表单元的HorizontalAlignment被忽略/断开

来自分类Dev

在PostgreSQL中不起作用

来自分类Dev

imagemagick -contrast-stretch不起作用(v6.7)

来自分类Dev

IE CSS不起作用,但在Firefox中起作用

来自分类Dev

WPF将HorizontalAlignment绑定到TextAlignment

来自分类Dev

在DataGridTextColumn中的ComboBox上,HorizontalAlignment不起作用

来自分类Dev

python treeview列“ stretch = False”不起作用

来自分类Dev

XAML HorizontalAlignment属性无法满足我的要求

来自分类Dev

当设置'HorizontalAlignment =“ Stretch”'时,如何获取'Width'?

来自分类Dev

$(this)在if($(“。element”)。is()中不起作用

来自分类Dev

为什么在扩展TreeViewItem时调用命令不起作用?

来自分类Dev

QtGui.QSystemTrayIcon settooltip在Debian Stretch中不起作用

来自分类Dev

UWP 使用 VisualStateManager 将 Grid.HorizontalAlignment 更改为拉伸/向左

来自分类Dev

WPF - 在页面上将 HorizontalAlignment 设置为 Left 时拉伸扩展器

来自分类Dev

TreeViewItem 的 IsSelected 属性的触发器在 xaml 中不起作用?

来自分类Dev

%% 不起作用,但 % 在库依赖项中起作用

来自分类Dev

HorizontalAlignment 或 VerticalAlignment 无法按预期工作

Related 相关文章

  1. 1

    UPDATE语句在sqlite中不起作用

  2. 2

    Ajax在Wordpress管理中不起作用

  3. 3

    ScrollView的SubView中的UIButton不起作用

  4. 4

    将图例文本的HorizontalAlignment设置为八度的左侧

  5. 5

    Scalatest在IntelliJ 13.1中不起作用

  6. 6

    为什么我的flexbox flex-stretch属性不起作用?

  7. 7

    通用App ListView项目HorizontalAlignment

  8. 8

    HorizontalChildrenAlignment =“ Stretch”在WrapGrid中不起作用

  9. 9

    onbeforeunload在Windows Chrome中不起作用

  10. 10

    重复字符在PowerShell中不起作用

  11. 11

    XAML HorizontalAlignment属性无法满足我的要求

  12. 12

    在WPF中显示BitmapSource不起作用

  13. 13

    表单元的HorizontalAlignment被忽略/断开

  14. 14

    在PostgreSQL中不起作用

  15. 15

    imagemagick -contrast-stretch不起作用(v6.7)

  16. 16

    IE CSS不起作用,但在Firefox中起作用

  17. 17

    WPF将HorizontalAlignment绑定到TextAlignment

  18. 18

    在DataGridTextColumn中的ComboBox上,HorizontalAlignment不起作用

  19. 19

    python treeview列“ stretch = False”不起作用

  20. 20

    XAML HorizontalAlignment属性无法满足我的要求

  21. 21

    当设置'HorizontalAlignment =“ Stretch”'时,如何获取'Width'?

  22. 22

    $(this)在if($(“。element”)。is()中不起作用

  23. 23

    为什么在扩展TreeViewItem时调用命令不起作用?

  24. 24

    QtGui.QSystemTrayIcon settooltip在Debian Stretch中不起作用

  25. 25

    UWP 使用 VisualStateManager 将 Grid.HorizontalAlignment 更改为拉伸/向左

  26. 26

    WPF - 在页面上将 HorizontalAlignment 设置为 Left 时拉伸扩展器

  27. 27

    TreeViewItem 的 IsSelected 属性的触发器在 xaml 中不起作用?

  28. 28

    %% 不起作用,但 % 在库依赖项中起作用

  29. 29

    HorizontalAlignment 或 VerticalAlignment 无法按预期工作

热门标签

归档