【问题标题】:C# WPF specify folders in XAML styleC# WPF 以 XAML 样式指定文件夹
【发布时间】:2019-01-20 06:43:14
【问题描述】:

我有一个具有以下结构的项目:

Proj
├──Views
├   ├──Dashboard.xaml
├   ├──Dashboard.cs
├
├──Styles
    ├──DashboardStyle.xaml

在我的DashboardStyle.xaml,我有这个代码:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:Proj.Styles">

    <Style x:Key="MyWindowStyle" TargetType="local:Proj/Views/Dashboard">
        ....
        ....
    </Style>

</ResourceDictionary>

但它给出了错误:

命名空间“clr-namespace:Proj.Styles”中不存在名称“Proj/Views/Dashboard”

我该如何解决这个问题?

【问题讨论】:

  • 不应该 localclr-namespace:Proj.Views 吗?然后TargetType 变为local:Dashboard

标签: c# wpf xaml resourcedictionary


【解决方案1】:

使用命名空间和类型名称引用类型,而不是通过物理文件路径。

所以要引用类型Proj.Views.Dashboard,添加相应的命名空间作为XML命名空间声明并在TargetType属性中使用它,例如

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:Proj.Styles"
                xmlns:views="clr-namespace:Proj.Views" >

    <Style x:Key="MyWindowStyle" TargetType="views:Dashboard">
        ....
        ....
    </Style>

</ResourceDictionary>

【讨论】:

  • 但我现在遇到了另一个问题...我在同一个文件中也有标题栏按钮样式&lt;Style x:Key="WindowButtonStyle" TargetType="{x:Type Button}"&gt;。按钮正在获取样式,但在悬停时不会改变颜色,或者在点击时不会改变
  • 其他问题,请再问。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-26
相关资源
最近更新 更多