【问题标题】:I can't change the fontsize on Listview items or column headers我无法更改 Listview 项目或列标题的字体大小
【发布时间】:2012-01-16 20:28:34
【问题描述】:
    <ListView x:Name="lstProductionOrders">
         <ListView.View>
              <GridView>
                   <GridViewColumn Header="Production Order:"/>
                        ...

我已经在网格上设置了字体大小,使用 textelement,我已经在 listview、columnheader、通过样式、通过使用 BasedOn 的样式设置了它。我什至放入了一个全新的测试窗口,其中没有其他代码,但默认代码和列表视图以上面相同的方式创建。使用上述所有方法,字体会在设计时更改,但不会在运行时更改。

我只有大约 6 个月的 WPF/XAML 经验,所以我可能正在寻找错误的东西。是否有某种字体大小的全局设置或任何任何人都可以想到的会导致它被覆盖的设置。

为了记录,我用测试窗口,没有基类。

---------编辑----------

这是 App 中问题的根源。 xml

<Style TargetType="TextBlock">
        <Setter Property="FontFamily" Value="Times New Roman"/>
        <Setter Property="FontSize" Value="20"/>
</Style>

【问题讨论】:

    标签: wpf xaml listview styles font-size


    【解决方案1】:

    对我来说,以下内容适用于标题和项目:

    <ListView ItemsSource="{Binding Source={StaticResource Items}}"
              TextElement.FontSize="16">
        <!-- ... -->
    

    不知道你的情况可能有什么问题......

    【讨论】:

      【解决方案2】:

      如果您只想设置标题大小,请尝试此解决方案 或使用@H.B.解决方案

      <ListView x:Name="lstProductionOrders" >
          <ListView.View>
              <GridView>
                  <GridViewColumn>
                      <GridViewColumnHeader TextElement.FontSize="16">Production Order:/GridViewColumnHeader>
                  </GridViewColumn>
              </GridView>
          </ListView.View>
      </ListView>
      

      希望对你有帮助

      【讨论】:

      • @Xcalibur37 就像一个魅力!在我的机器上™(我已经用截图更新了我的答案)
      • 抱歉敲你的答案。如果不直接将 TextBlock 放入 GridViewColumn 标题的内容中,我将无法使其工作。这在 GridView 和 DataGrid 中并非闻所未闻。这是 WPF 需要做得更好的事情。
      【解决方案3】:

      这听起来很像您对 GridViewColumn 有一个隐式样式,它会覆盖您在本地所做的更改。

      在设计时,不考虑隐式样式(可能出于各种原因),但在运行时会考虑,并且会出现奇怪的行为。

      我会先查看 App.xaml 的 Resources 部分,然后向上移动,直到找到我关心的控件。

      补充:隐式样式:

      <UserControl>
         <UserControl.Resources>
            <Style TargetType="TextBlock">
               <Setter Property="FontSize" Value="40" /> <!-- stupid value, just to make it obvious it changed something =) -->
            </Style>
         </UserControl.Resources>
      </UserControl>
      

      如果你想在本地定义一个隐式样式而不覆盖链上更高的另一个隐式样式,你可以这样做:

      <UserControl>
         <UserControl.Resources>
            <Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
               <Setter Property="FontSize" Value="60" /> <!-- ridiculous value, just to make it obvious it changed something =) -->
            </Style>
         </UserControl.Resources>
      </UserControl>
      

      它将继承隐式样式并为本地 UserControl 添加更改。

      【讨论】:

      • 不错! TextBlock 样式在 App.xaml 中使用 FontSize 集定义。我从没想过要为 Listview 标题和项目寻找 TextBlock。很棒的建议。再次感谢!
      • 您还可以在本地控件中为 TextBlock 定义另一种隐式样式。它将覆盖 App.xaml 中设置的全局隐式样式
      • 该死的,伙计。把各种知识都丢在我身上。我刚刚开始研究如何覆盖它。再次感谢。谁能想到狒狒会对 XAML 了解这么多。
      • 很高兴它有帮助 =) 隐式样式只是没有 x:Key 的样式,它们将应用于已设置的 TargetType 的任何控件。
      • 我尝试了您的覆盖解决方案,但没有奏效。在 Window、Grid 和 ListView 级别的 Resources 属性上进行了尝试。我是否必须提供指向 App.xaml 的任何“路径”信息?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-07
      相关资源
      最近更新 更多