我用代码创建了一个新的Calendar控件:
Calendar calendar = new Calendar();我想用我的app.xaml中定义的样式来设置它的CalendarDayButtonStyle
calendar.CalendarDayButtonStyle = ...但我不知道该怎么做。另外,我不能访问"FindResource“,这是我见过的其他人使用的。(我在一个ICommand中,所以我不能访问这个方法)还有其他方法吗?
发布于 2012-01-10 03:39:15
您可以使用Application.Current.FindResource。
发布于 2012-01-10 08:59:44
这是非常直接的。
在应用程序中实例化ResourceDictionary
App.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/AssemplyOfResource;Component/Resource.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>在代码中检索样式
CodeBehind
object resource = Application.Current.FindResource("KeyName");
if (resource != null && resource.GetType() == typeof(Style))
calendar.Style = (Style)resource;你会看到大多数人只是使用Application.Current.FindResource("KeyName")并转换它,这是很好的,除非图形设计师决定搞乱ResourceDictionaries。这将导致应用程序不会崩溃。
如果您需要一些特定的Calandar示例,请访问:
MSDN Magazine Customizing the New WPF Calendar Controls
https://stackoverflow.com/questions/8794112
复制相似问题