在WPF中自定义控制到Window应用程序

时间:2023-02-19 19:44:27

the title isn't really meaningfull... sorry for this.I'll try to explain my issue. I have a bigger project in which i'm trying to implement a new WPF window which contains some custom controls. Going deeper this window should looks like, let's say, a calculator. This calculators has many keys buttons which are "custom controls" inherited from Controls, and their style are defined in Generic.xaml. When i try to debug it, the custom controls (the key buttons) don't appear into the calculator window. I think it's due to something wrong with the Generic.xaml file. Can someone give me a hint ??? Thanks in advance Paolo

标题不是真的有意义......抱歉。我会试着解释我的问题。我有一个更大的项目,我正在尝试实现一个包含一些自定义控件的新WPF窗口。更深入的这个窗口看起来应该是一个计算器。这个计算器有许多键按钮,它们是从Controls继承的“自定义控件”,它们的样式在Generic.xaml中定义。当我尝试调试它时,自定义控件(键按钮)不会出现在计算器窗口中。我认为这是由于Generic.xaml文件出了问题。有人能给我一个暗示吗?在此先感谢Paolo

2 个解决方案

#1


0  

A couple of suggestions:

一些建议:

  • download Snoop (it's free), it examines your Visual tree at run time and helps to see if controls are there, if the style making them invisible, etc.

    下载Snoop(它是免费的),它在运行时检查您的Visual树,并帮助查看是否有控件,如果样式使它们不可见,等等。

  • remove the styles, run your project - do controls appear? then it's your styles!

    删除样式,运行项目 - 是否出现控件?那是你的风格!

  • do you have nameless/key-less styles that target buttons, other simple/out-of-the-box control? Then remove them too, if they are the ones affecting your controls, then consider changing them, or applying styles to your controls so they stop inheriting from those defaults.

    你有无名/无键样式的目标按钮,其他简单/开箱即用的控制?然后删除它们,如果它们是影响控件的那些,则考虑更改它们,或者将样式应用于控件,以便它们停止继承这些默认值。

#2


0  

I've searched a lot on the net and finally i've got the solution following the answer of this discussion wpf-resource-not-loading-from-generic. The clue is to add into the AssemblyInfo.cs these lines of code:

我在网上搜索了很多,最后我得到了这个讨论的答案wpf-resource-not-loading-from-generic。线索是将这些代码行添加到AssemblyInfo.cs中:

[assembly: ThemeInfo(
    ResourceDictionaryLocation.None, 
    //where theme specific resource dictionaries are located
    //(used if a resource is not found in the page, 
    // or application resource dictionaries)
    ResourceDictionaryLocation.SourceAssembly 
    //where the generic resource dictionary is located
    //(used if a resource is not found in the page, 
    // app, or any theme specific resource dictionaries)
)]

Maybe this could be useful for someone in the future... :-)

也许这对未来的某个人有用...... :-)

Regards, Paolo

#1


0  

A couple of suggestions:

一些建议:

  • download Snoop (it's free), it examines your Visual tree at run time and helps to see if controls are there, if the style making them invisible, etc.

    下载Snoop(它是免费的),它在运行时检查您的Visual树,并帮助查看是否有控件,如果样式使它们不可见,等等。

  • remove the styles, run your project - do controls appear? then it's your styles!

    删除样式,运行项目 - 是否出现控件?那是你的风格!

  • do you have nameless/key-less styles that target buttons, other simple/out-of-the-box control? Then remove them too, if they are the ones affecting your controls, then consider changing them, or applying styles to your controls so they stop inheriting from those defaults.

    你有无名/无键样式的目标按钮,其他简单/开箱即用的控制?然后删除它们,如果它们是影响控件的那些,则考虑更改它们,或者将样式应用于控件,以便它们停止继承这些默认值。

#2


0  

I've searched a lot on the net and finally i've got the solution following the answer of this discussion wpf-resource-not-loading-from-generic. The clue is to add into the AssemblyInfo.cs these lines of code:

我在网上搜索了很多,最后我得到了这个讨论的答案wpf-resource-not-loading-from-generic。线索是将这些代码行添加到AssemblyInfo.cs中:

[assembly: ThemeInfo(
    ResourceDictionaryLocation.None, 
    //where theme specific resource dictionaries are located
    //(used if a resource is not found in the page, 
    // or application resource dictionaries)
    ResourceDictionaryLocation.SourceAssembly 
    //where the generic resource dictionary is located
    //(used if a resource is not found in the page, 
    // app, or any theme specific resource dictionaries)
)]

Maybe this could be useful for someone in the future... :-)

也许这对未来的某个人有用...... :-)

Regards, Paolo