C# BasedOn=”等;{StaticResource{x:Type TextBox}}";在代码背后的风格
如何在代码隐藏中设置以下内容C# BasedOn=”等;{StaticResource{x:Type TextBox}}";在代码背后的风格,c#,wpf,xaml,styles,basedon,C#,Wpf,Xaml,Styles,Basedon,如何在代码隐藏中设置以下内容 <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}"> 有什么建议吗?另外,是否有任何方法可以在不使用BasedOn的情况下应用主题样式而不是默认样式 谢谢这应该可以: Style baseStyle = new Style(typeof(TextBox)); Style editingStyle = new Style(typeof(TextB
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
有什么建议吗?另外,是否有任何方法可以在不使用BasedOn的情况下应用主题样式而不是默认样式
谢谢这应该可以:
Style baseStyle = new Style(typeof(TextBox));
Style editingStyle = new Style(typeof(TextBox));
editingStyle.BasedOn = baseStyle;
也可以在构造函数中执行此操作:
Style editingStyle = new Style(typeof(TextBox), baseStyle);
试试这个:
editingStyle.BasedOn = (Style) FindResource(typeof (TextBox))
我不知道如何才能让它在不指定BasedOn
的情况下应用主题样式。如果有这样的方法,我也想知道它…我喜欢,但它不编译
FindResource是。
需要识别搜索请求的上下文
因此,我建议:
style.BasedOn = (Style)frameworkElement.FindResource(typeof(TextBox));
谢谢你的回答!是的,我希望这能起作用,但由于某种原因,主题风格没有被采用。如果我在Xaml中这样做,它会起作用。你试图将主题应用于给定的控件,并让本地样式覆盖它影响的主题组件?@Aaron:我使用的主题会影响所有控件。但在许多地方,我需要添加特定的setter、触发器等。例如,绑定
ListBoxItem.IsSelected
在ListBox
ItemContainerStyle中。我想用一个附加的setter应用主题样式
style.BasedOn = (Style)frameworkElement.FindResource(typeof(TextBox));