如何将现代UI菜单文本改为大写

6

我正在使用WPF的Modern UI创建一个项目。主菜单项似乎都是小写字母,这是我想要改变的主题中唯一的一件事情。有没有办法从我的项目的MainWindow.xamlMainWindow.xaml.cs或其他任何文件中进行更改?

我的菜单代码如下:

<mui:LinkGroup DisplayName="Home" >
    <mui:LinkGroup.Links>
        <mui:Link DisplayName="Dashboard" Source="/Pages/home.xaml" />
    </mui:LinkGroup.Links>
</mui:LinkGroup>

FYI,我可以从主题代码中更改它并构建一个新的FirstFloor.ModernUI.dll文件并使用它。但这不是我想要的,如果我不能在使用一个.dll后覆盖它,那么它将无效。一定有一种方法,我一定错过了它。
更新: 我有一个显示窗口的图像。
我没有关于DASHBOARD的问题,但我想做的是将home更改为大写,或者我如何在xaml代码上书写。

你说的小写是什么意思?您不提供标题文本吗?您是说主页显示为home,仪表板显示为dashboard吗? - eran otzap
是的,我在说同样的事情。我提供了“DisplayName”,但它总是显示为小写。 - Luzan Baral
这似乎是在Link控件内部的某个地方将所有内容转换为小写。您需要创建一个继承自Link控件的自定义控件。然后,您可以重写隐藏DisplayName依赖属性。 - Mike Eason
我同意Mike的观点,你应该查看Link内部并搜索Display name,看看是谁设置了它。 - eran otzap
你能否不暴露控件的样式模板,以便查看绑定是否不像从模板中的绑定基础上简单地拉出转换器那么容易?比如说,是否有一个包含XAML模板的ModernUI.xaml资源字典? - Chris W.
2个回答

4

如果您查看MUI项目的源代码,会发现一个名为ModernMenu.xaml的主题(在FirstFloor.ModernUI项目的Themes下)。

您可以像以下这样简单地向您自己的应用程序添加一个样式。(我删除了将文本设置为小写的转换器,并增加了第一行菜单选项之间的间距,以便多个单词的选项与相邻选项清晰分离。)

<Style TargetType="controls:ModernMenu">
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="controls:ModernMenu">
                <Grid>
                    <Grid.Resources>
                        <Style TargetType="ListBox" BasedOn="{StaticResource {x:Type ListBox}}">
                            <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden"/>
                            <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden"/>
                            <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                            <Setter Property="ScrollViewer.PanningMode" Value="Both"/>
                        </Style>
                    </Grid.Resources>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="40" />
                        <RowDefinition Height="16" />
                    </Grid.RowDefinitions>

                    <ListBox ItemsSource="{TemplateBinding VisibleLinkGroups}"
                     SelectedItem="{Binding SelectedLinkGroup, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}">
                        <ListBox.ItemContainerStyle>
                            <Style TargetType="ListBoxItem">
                                <Setter Property="FocusVisualStyle" Value="{x:Null}" />
                                <Setter Property="FontFamily" Value="Segoe UI Light" />
                                <Setter Property="Foreground" Value="{DynamicResource MenuText}" />
                                <Setter Property="FontSize" Value="23"/>
                                <Setter Property="HorizontalContentAlignment" Value="Center" />
                                <Setter Property="VerticalContentAlignment" Value="Center" />
                                <Setter Property="TextOptions.TextFormattingMode" Value="Ideal" />
                                <Setter Property="Margin" Value="0,0,25,0" />
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="ListBoxItem">
                                            <TextBlock DataContext="{TemplateBinding Content}"
                                               Text="{Binding DisplayName}"
                                               HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                               VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                               SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                            <ControlTemplate.Triggers>
                                                <Trigger Property="IsMouseOver" Value="true">
                                                    <Setter Property="Foreground" Value="{DynamicResource MenuTextHover}"/>
                                                </Trigger>
                                                <Trigger Property="IsSelected" Value="true">
                                                    <Setter Property="Foreground" Value="{DynamicResource MenuTextSelected}"/>
                                                </Trigger>
                                            </ControlTemplate.Triggers>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </ListBox.ItemContainerStyle>
                        <ListBox.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel Orientation="Horizontal" />
                            </ItemsPanelTemplate>
                        </ListBox.ItemsPanel>
                    </ListBox>

                    <ListBox Grid.Row="1"
                     ItemsSource="{Binding SelectedLinkGroup.Links, RelativeSource={RelativeSource TemplatedParent}}"
                     SelectedItem="{Binding SelectedLink, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                     VerticalAlignment="Top">
                        <ListBox.ItemContainerStyle>
                            <Style TargetType="ListBoxItem">
                                <Setter Property="FocusVisualStyle" Value="{x:Null}" />
                                <Setter Property="FontFamily" Value="Segoe UI" />
                                <Setter Property="Foreground" Value="{DynamicResource SubMenuText}" />
                                <Setter Property="FontSize" Value="11"/>
                                <Setter Property="Margin" Value="0,0,12,0" />
                                <Setter Property="HorizontalContentAlignment" Value="Center" />
                                <Setter Property="VerticalContentAlignment" Value="Center" />
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="ListBoxItem">
                                            <Grid DataContext="{TemplateBinding Content}"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                                                <TextBlock Text="{Binding DisplayName, Converter={StaticResource ToUpperConverter}}" TextAlignment="Center"/>
                                                <TextBlock Text="{Binding DisplayName, Converter={StaticResource ToUpperConverter}}" FontWeight="Bold" Visibility="Hidden" />
                                            </Grid>

                                            <ControlTemplate.Triggers>
                                                <Trigger Property="IsMouseOver" Value="true">
                                                    <Setter Property="Foreground" Value="{DynamicResource SubMenuTextHover}"/>
                                                </Trigger>
                                                <Trigger Property="IsSelected" Value="true">
                                                    <Setter Property="Foreground" Value="{DynamicResource SubMenuTextSelected}"/>
                                                    <Setter Property="FontWeight" Value="Bold" />
                                                </Trigger>
                                            </ControlTemplate.Triggers>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </ListBox.ItemContainerStyle>
                        <ListBox.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel Orientation="Horizontal" />
                            </ItemsPanelTemplate>
                        </ListBox.ItemsPanel>
                    </ListBox>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

为了做到这一点,您需要在XAML文件顶部引用命名空间:
xmlns:controls="clr-namespace:FirstFloor.ModernUI.Windows.Controls;assembly=FirstFloor.ModernUI"

那应该就可以了。

如何更改LinkGroup.Links的样式? - Raj
对我没有用。我应该把它放在哪里?我在我的主窗口中包含了样式,但没有任何反应。 - Gaspa79

3
我遇到了与上述描述相同的问题。
似乎包含链接的“ModernMenu”将“DisplayName”值转换为小写。
通过Blend的帮助,我发现基本的“ControlTemplate”包含一个“TextBlock”,其中绑定到“DisplayNameProperty”。
为了解决这个问题,我为“ModernMenu”创建了一个新的“ControlTemplate”,基于基本的“ModernMenu ControlTemplate”,但没有使用“BindingConverter”。不幸的是,当我定义自定义“ControlTemplate”时,整个控件都不可见或无法绘制,所以这个方法不起作用。
在我看来,目前没有简单改变“DisplayNameProperty”的样式的方法。我花费了很多时间寻找解决方案,但每次尝试都失败了。
也许从“ModernMenu”继承并基于没有转换器的“ModernMenu”创建一个新的“ControlTemplate”的自定义控件会起作用。
我将在接下来的几天内进行测试,并发布我的尝试经验。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接