Stretch ToolbarTray - ширина его строки - PullRequest
3 голосов
/ 29 марта 2011

Панель инструментов находится в первом ряду.Как вы можете растянуть панель инструментов на ширину всего ряда?

<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>

Ответы [ 5 ]

3 голосов
/ 29 марта 2011

Я удалил ToolbarTray и установил HorizontalContentAlignment = "Strech", это сделал

3 голосов
/ 30 марта 2011

Если вы оставите ToolBarTray, вы можете использовать привязку к свойству ToolBar.Width следующим образом:

<ToolBarTray Background="Transparent">
    <!--Toolbar.xaml-->
    <ToolBar Width="{Binding ActualWidth,
                             RelativeSource={RelativeSource FindAncestor,
                                           AncestorType={x:Type ToolBarTray}}}">
      <Button>Hello</Button>
    </ToolBar>
</ToolBarTray>
1 голос
/ 24 февраля 2013

Вот как я это сделал:

Сначала я удалил панель инструментовTray и сохранил панель инструментов

<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"/>

При этом ваши кнопки будут выровнены по правому краю

1 голос
/ 29 марта 2011

Вы пробовали выравнивание по горизонтали?

<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>
0 голосов
/ 02 апреля 2018

Используйте ширину родителя, например,

    <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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...