在样式的代码背后的BasedOn ="{StaticResource {x:Type TextBox}}"

Fre*_*lad 19 c# wpf xaml styles basedon

如何在代码中设置以下内容?

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
Run Code Online (Sandbox Code Playgroud)

我正在使用App.xaml中合并的主题.它适用于所有控件,但是当我为某些东西定义样式时,例如TextBox,除非我BasedOn像上面一样使用主题样式,否则它将获得默认TextBox样式.

现在我正在创建一个DataGridTextColumn代码背后,我无法让它的BasedOn部分工作EditingElementStyle

Style editingStyle = new Style(typeof(TextBox));
editingStyle.BasedOn = ...?;
Run Code Online (Sandbox Code Playgroud)

有什么建议?此外,有没有办法获得主题样式而不是使用默认样式而不使用BasedOn?

谢谢

Pav*_*kov 25

试试这个:

editingStyle.BasedOn = (Style) FindResource(typeof (TextBox))
Run Code Online (Sandbox Code Playgroud)

而且我不知道如何使它在没有指定的情况下应用主题样式BasedOn.如果有这样的方式,我也想知道它...


Edw*_*ing 5

这应该工作:

Style baseStyle = new Style(typeof(TextBox));
Style editingStyle = new Style(typeof(TextBox));
editingStyle.BasedOn = baseStyle;
Run Code Online (Sandbox Code Playgroud)

您也可以在构造函数中执行此操作:

Style editingStyle = new Style(typeof(TextBox), baseStyle);
Run Code Online (Sandbox Code Playgroud)