【问题标题】:how to load my style file instead of generic.xaml while applying style in window wpf如何在窗口 wpf 中应用样式时加载我的样式文件而不是 generic.xaml
【发布时间】:2018-11-07 09:10:19
【问题描述】:

我有 WPF 项目,在该项目中,我在整个应用程序的窗口中应用了另一个程序集的样式。但是,我在应用程序内部使用的一个自定义控件没有采用这种样式。它的样式总是从 generic.xaml 加载。

在这个自定义控件类中,我设置了 DefaultStyleKey 属性,并将外部样式文件添加到应用程序资源字典的合并字典中。任何人都可以面对这种问题吗?

注意:其他控件的样式是从另一个组件中应用的

<Window x:Class="WpfApplication_CalenderEdit.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:WpfApplication_CalenderEdit"   
    xmlns:thirdParty=""
    mc:Ignorable="d" thirdParty.Style="ThemeNew"
    xmlns:ie="http://schemas.microsoft.com/expression/2010/interactivity"
    WindowStartupLocation="CenterScreen" 
  Title="Calendar Sample" Height="450" Width="625"  >
<!--<Window.DataContext>
    <local:ViewModel/>

</Window.DataContext>-->
<Grid  >
    <!--<DockPanel>-->
    <StackPanel >

        <Calendar WeekNumberForeground="Red" Padding="10"  />
      <ComboBox Margin="20" Height="35" Width="150"/>
    </StackPanel>

【问题讨论】:

  • 您是如何“在整个应用程序的窗口中应用另一个程序集的样式”的?对自定义控件执行相同操作。
  • 我的主题文件在第三方库中。我将它的程序集包含在我的项目中,并在窗口中全局设置样式

标签: wpf styles themes


【解决方案1】:

您也应该为您的自定义窗口显式应用相同的Style,例如:

<Style TargetType="{x:Type Window}" BasedOn="{StaticResource CustomStyle}" />
<Style TargetType="{x:Type controls:CustomWindow}" BasedOn="{StaticResource CustomStyle}" />

【讨论】:

  • 我没有使用自定义窗口,我在窗口中设置了样式,从该样式将应用于窗口内定义的所有控件
  • @Keerthana:请发布您的 XAML 以澄清您的问题。
  • 发布了 xaml 代码,当它直接设置到控件示例时也会应用样式:
猜你喜欢
  • 2012-06-24
  • 1970-01-01
  • 1970-01-01
  • 2019-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-01
相关资源
最近更新 更多