我有一个使用一些PART控件的自定义控件:
[TemplatePart(Name = "PART_TitleTextBox", Type = typeof(TextBox))]
[TemplatePart(Name = "PART_TitleIndexText", Type = typeof(Label))]
[TemplatePart(Name = "PART_TimeCodeInText", Type = typeof(TextBlock))]
[TemplatePart(Name = "PART_TimeCodeOutText", Type = typeof(TextBlock))]
[TemplatePart(Name = "PART_ApprovedImage", Type = typeof(Image))]
[TemplatePart(Name = "PART_CommentsImage", Type = typeof(Image))]
[TemplatePart(Name = "PART_BookmarkedImage", Type = typeof(Image))]
public class TitleBoxNew : Control
{
static TitleBoxNew()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof(TitleBoxNew),
new FrameworkPropertyMetadata(typeof(TitleBoxNew)));
}
public TitleBoxNew() { }
// ... rest of class
}
Run Code Online (Sandbox Code Playgroud)
此控件覆盖OnApplyTemplate:
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
InitializeEvents();
}
Run Code Online (Sandbox Code Playgroud)
大部分时间都适用.我已经在窗口中的自定义选项卡控件中添加了控件,并且不会以某种方式为该控件调用OnApplyTemplate!为什么这不能像我期望的那样工作?
Kai*_*i G 40
对于任何可能偶然发现这篇文章的人,我遇到了同样的问题,我设法通过将以下内容添加到包含我的自定义控件的项目的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)
)]
Run Code Online (Sandbox Code Playgroud)
我的控件的模板位于与控件相同的项目中的Theme/Generic.xaml文件中.
MoM*_*oMo 26
另外两个答案是正确的......但不完整.根据这篇文章(以及我刚刚解决这个问题的经验),你需要检查4件事情:(由于某种原因,如果我使用数字或破折号,这篇文章中的代码块将不会保持格式化...所以写信给它是)
答:控件模板和样式应位于Generic.xaml
文件中,该文件夹名为Themes
项目根目录.
B.确保您的命名空间正确 Generic.xaml
C.在控件的构造函数中设置样式键.还广泛建议您将以下内容放在静态构造函数中.
static YourControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(YourControl), new FrameworkPropertyMetadata(typeof(YourControl)));
}
Run Code Online (Sandbox Code Playgroud)
D.确保您的assemblyinfo.cs中包含以下内容
[assembly: ThemeInfo(ResourceDictionaryLocation.None,
//where theme specific resource dictionaries are located
//(used if a resource is not found in the
// 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)
)]
Run Code Online (Sandbox Code Playgroud)
我看不到你的构造函数,但不要忘记设置 DefaultStyleKey:
DefaultStyleKeyProperty.OverrideMetadata(typeof(TitleBoxNew), new FrameworkPropertyMetadata(typeof(TitleBoxNew)));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12851 次 |
最近记录: |