将 ToolbarTray 拉伸为其 Row 的宽度

发布于 2024-10-27 16:33:46 字数 803 浏览 2 评论 0原文

工具栏位于第一行。如何将工具栏拉伸到整行的宽度?

<Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

<ToolBarTray Background="Transparent">
        <!--Toolbar.xaml-->
        <ToolBar x:Name="mainToolbar" 
                 Style="{StaticResource mainToolBarStyle}"
                 ItemsSource="{Binding ToolbarItems}"
                 ItemTemplateSelector="{StaticResource toolBarItemTemplateSelector}"
                 DataContext="{Binding}">
        </ToolBar>
    </ToolBarTray>

A toolbar is on the first row. How can you stretch the toolbar to be the width of the entire row?

<Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

<ToolBarTray Background="Transparent">
        <!--Toolbar.xaml-->
        <ToolBar x:Name="mainToolbar" 
                 Style="{StaticResource mainToolBarStyle}"
                 ItemsSource="{Binding ToolbarItems}"
                 ItemTemplateSelector="{StaticResource toolBarItemTemplateSelector}"
                 DataContext="{Binding}">
        </ToolBar>
    </ToolBarTray>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

阳光下慵懒的猫 2024-11-03 16:33:46

我删除了 ToolbarTray 并设置了 Horizo​​ntalContentAlignment="Stretch"

I removed the ToolbarTray and set HorizontalContentAlignment="Stretch".

任谁 2024-11-03 16:33:46

如果您要离开 ToolBarTray,您可以在 ToolBar.Width 属性上使用绑定,如下所示:

<ToolBarTray Background="Transparent">
    <!--Toolbar.xaml-->
    <ToolBar Width="{Binding ActualWidth,
                             RelativeSource={RelativeSource FindAncestor,
                                           AncestorType={x:Type ToolBarTray}}}">
      <Button>Hello</Button>
    </ToolBar>
</ToolBarTray>

If you were to leave the ToolBarTray you can use a binding on the ToolBar.Width property like so:

<ToolBarTray Background="Transparent">
    <!--Toolbar.xaml-->
    <ToolBar Width="{Binding ActualWidth,
                             RelativeSource={RelativeSource FindAncestor,
                                           AncestorType={x:Type ToolBarTray}}}">
      <Button>Hello</Button>
    </ToolBar>
</ToolBarTray>
云胡 2024-11-03 16:33:46

您尝试过水平内容对齐吗?

<ToolBarTray Background="Transparent" Width="Auto">
    <!--Toolbar.xaml-->  
    <ToolBar x:Name="mainToolbar" HorizontalContentAlignment="Stretch"  
             Style="{StaticResource mainToolBarStyle}"  
             ItemsSource="{Binding ToolbarItems}"  
             ItemTemplateSelector="{StaticResource toolBarItemTemplateSelector}"  
             DataContext="{Binding}">  
    </ToolBar>  
</ToolBarTray>

have you tried horizontalcontentalignment?

<ToolBarTray Background="Transparent" Width="Auto">
    <!--Toolbar.xaml-->  
    <ToolBar x:Name="mainToolbar" HorizontalContentAlignment="Stretch"  
             Style="{StaticResource mainToolBarStyle}"  
             ItemsSource="{Binding ToolbarItems}"  
             ItemTemplateSelector="{StaticResource toolBarItemTemplateSelector}"  
             DataContext="{Binding}">  
    </ToolBar>  
</ToolBarTray>
似最初 2024-11-03 16:33:46

我就是这样做的:

首先,我删除了工具栏托盘并保留了工具栏

<ToolBar>
                <Button x:Name="OpenTabsButton1" Click="OpenTabs_Click" Style="{StaticResource OpenTabs}">Open Tabs</Button>
                <Button x:Name="ShortcutsButton1" Click="OpenTabs_Click" Style="{StaticResource Shortcuts}">Shortcuts</Button>
                <Button x:Name="HelpButton1" Click="OpenTabs_Click" Style="{StaticResource Help}">Help</Button>
                <Button x:Name="ProfileButton1" Click="Profile_Click" Style="{StaticResource Profile}">Profile</Button>
                <Button Style="{DynamicResource CloseButton}"></Button>
            </ToolBar>

然后,我为工具栏创建了一个样式

    <Style x:Key="{x:Type ToolBar}" TargetType="{x:Type ToolBar}">
    <Setter Property="SnapsToDevicePixels" Value="true" />
    <Setter Property="OverridesDefaultStyle" Value="true" />
    <Setter Property="HorizontalAlignment" Value="Stretch"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ToolBar}">
                <Grid Background="{StaticResource ToolGridBackground}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <Image Grid.Column="0" Style="{StaticResource LogoImage}"/>
                    <ToolBarPanel Grid.Column="2" x:Name="PART_ToolBarPanel" IsItemsHost="true" Margin="0,1,2,2" Orientation="Horizontal"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

重要的部分是:

<Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>

有了

<ToolBarPanel Grid.Column="2"/>

这个,你的按钮将右对齐

This is how I did it:

First, I removed the toolbarTray and kept the toolbar

<ToolBar>
                <Button x:Name="OpenTabsButton1" Click="OpenTabs_Click" Style="{StaticResource OpenTabs}">Open Tabs</Button>
                <Button x:Name="ShortcutsButton1" Click="OpenTabs_Click" Style="{StaticResource Shortcuts}">Shortcuts</Button>
                <Button x:Name="HelpButton1" Click="OpenTabs_Click" Style="{StaticResource Help}">Help</Button>
                <Button x:Name="ProfileButton1" Click="Profile_Click" Style="{StaticResource Profile}">Profile</Button>
                <Button Style="{DynamicResource CloseButton}"></Button>
            </ToolBar>

Then, I created a style for the toolbar

    <Style x:Key="{x:Type ToolBar}" TargetType="{x:Type ToolBar}">
    <Setter Property="SnapsToDevicePixels" Value="true" />
    <Setter Property="OverridesDefaultStyle" Value="true" />
    <Setter Property="HorizontalAlignment" Value="Stretch"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ToolBar}">
                <Grid Background="{StaticResource ToolGridBackground}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <Image Grid.Column="0" Style="{StaticResource LogoImage}"/>
                    <ToolBarPanel Grid.Column="2" x:Name="PART_ToolBarPanel" IsItemsHost="true" Margin="0,1,2,2" Orientation="Horizontal"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

The important part is :

<Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>

And

<ToolBarPanel Grid.Column="2"/>

With this, your buttons will be right aligned

一绘本一梦想 2024-11-03 16:33:46

使用父级的宽度,例如

    <Grid x:Name="GrdToolbar" Height="auto" Margin="0">
    <ToolBarTray Width="{Binding Path=ActualWidth, ElementName=GrdToolbar}" >
        <ToolBar ItemsSource="{Binding MenuItems}" HorizontalContentAlignment="Stretch">
            <ToolBar.ItemTemplate>
                <DataTemplate>
                    <Button ToolTip="{Binding Name}" Tag ="{Binding Name}"
                            Command="{Binding DataContext.ButtonCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}}"
                            CommandParameter="{Binding RelativeSource={RelativeSource Self}}">
                        <Button.Content>
                            <Image Width="32" Height="32" Source="{Binding Icon}"></Image>
                        </Button.Content>
                    </Button>
                </DataTemplate>
            </ToolBar.ItemTemplate>
        </ToolBar>
    </ToolBarTray>
</Grid>

Use the parent's width, e.g.

    <Grid x:Name="GrdToolbar" Height="auto" Margin="0">
    <ToolBarTray Width="{Binding Path=ActualWidth, ElementName=GrdToolbar}" >
        <ToolBar ItemsSource="{Binding MenuItems}" HorizontalContentAlignment="Stretch">
            <ToolBar.ItemTemplate>
                <DataTemplate>
                    <Button ToolTip="{Binding Name}" Tag ="{Binding Name}"
                            Command="{Binding DataContext.ButtonCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}}"
                            CommandParameter="{Binding RelativeSource={RelativeSource Self}}">
                        <Button.Content>
                            <Image Width="32" Height="32" Source="{Binding Icon}"></Image>
                        </Button.Content>
                    </Button>
                </DataTemplate>
            </ToolBar.ItemTemplate>
        </ToolBar>
    </ToolBarTray>
</Grid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文