WPF MenuItem将样式应用于盒装标题

时间:2016-06-23 08:39:19

标签: c# wpf xaml menu

我目前正在尝试将样式应用到我的menuItem。我不得不将它的标题放在控件中,而我找到的唯一方法就是:

<MenuItem>
    <MenuItem.Header>
        <TextBlock Text="About" VerticalAlignment="Center" HorizontalAlignment="Center"/>
    </MenuItem.Header>
</MenuItem>

现在,我正在尝试应用样式,但我似乎无法绑定文本框&#39;从样式到实际xaml值的文本。

我的风格:

<Style x:Key="MenuItemBaseStyle" TargetType="MenuItem">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="Background" Value="#0a99f3" />
                    <Setter Property="Foreground" Value="White"/>
                </Trigger>
            </Style.Triggers>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type MenuItem}">
                        <Grid Background="{TemplateBinding Background}">
                            <MenuItem>
                                <MenuItem.Header>
                                    <TextBlock Text="{TemplateBinding Text}" />
                                </MenuItem.Header>
                            </MenuItem>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

TemplateBinding只是我尝试过的方法之一,但是没有一种方法可以远程关闭。

编辑 - 请求完整的XAML:

<Window x:Class="DownloadManager.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:DownloadManager"
        xmlns:controls="clr-namespace:DesignInControl"
        mc:Ignorable="d"
        Title="MainWindow" Height="700" Width="1000" Background="#22282a" ResizeMode="NoResize">
    <Border BorderThickness="1" BorderBrush="#ffcd22" Margin="10,10,10,10">
        <Grid>
            <controls:CircularProgressBar VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="0,0,10,10" SegmentColor="#ffcd22" StrokeThickness="5" Percentage="100" Radius="80" Grid.ZIndex="0"/>
            <Border VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="0,0,10,10" Width="170" Height="170">
                <TextBlock FontSize="60" Text="10%" FontFamily="simplifica" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Border>
            <Image x:Name="noshare_png" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="-8,10" Source="noshare.png" Width="100" Height="100"/>
            <Border Width="210" Height="100" Margin="97,10,665,540">
                <TextBlock Text="NoShare" FontSize="85" FontFamily="simplifica" Foreground="#ffcd22" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Border>
            <Menu x:Name="menu" HorizontalAlignment="Left" Height="100" Margin="352,10,0,0" VerticalAlignment="Top" Width="600" FontFamily="simplifica" FontSize="30">
                <MenuItem Height="100" Width="200" Style="{StaticResource MenuItemBaseStyle}">
                    <MenuItem.Header>
                        <TextBlock Text="File"/>
                    </MenuItem.Header>
                </MenuItem>
                <MenuItem Height="100" Width="200" Style="{StaticResource MenuItemBaseStyle}">
                    <MenuItem.Header>
                        <TextBlock Text="Settings"/>
                    </MenuItem.Header>
                </MenuItem>
                <MenuItem Height="100" Width="200" Style="{StaticResource MenuItemBaseStyle}">
                    <MenuItem.Header>
                        <TextBlock Text="About"/>
                    </MenuItem.Header>
                </MenuItem>
            </Menu>
        </Grid>
    </Border>
</Window>

1 个答案:

答案 0 :(得分:1)

使用MenuItem.HeaderTemplate

<Style x:Key="MenuItemBaseStyle" TargetType="MenuItem">
    <Setter Property="HeaderTemplate">
        <Setter.Value>
            <DataTemplate>
                <TextBlock Text="{Binding}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </DataTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="true">
            <Setter Property="Background" Value="#0a99f3" />
            <Setter Property="Foreground" Value="White"/>
        </Trigger>
    </Style.Triggers>
</Style>

更新:将您的风格与原始解决方案相结合。