将样式应用于派生自特定基数的控件 class
Apply Style to controls that derives from a certain base class
如何将样式应用于基于特定 CustomControl 的所有子项。
例子
自定义控件
public class SubView : UserControl
{ ... }
基于自定义控件的控件
public partial class MyView : SubView
{ ... }
XAML 为 MyView
<myLibrary:SubView
xmlns:myLibrary="....">
<Grid>
<!--Any content-->
</Grid>
</moduleChrome:SubView>
父级(此网格的子级是从代码运行时设置的)
<Grid>
<Grid.Resources>
<Style TargetType="myLibrary:SubView">
<Setter Property="MyCustomDependancy" Value="{binding to a shared MyCustomDependancy}"/>
</Style>
</Grid.Resources>
<myLibrary:SubView/> <!--This will have the shared MyCustomDependancy-->
<localFolder:MyView/> <!--But this will not be affected-->
</Grid>
如何让MyView受样式影响?
编辑
这段代码的起源是动态的并且相当复杂,但我试图让这个问题尽可能通用,以便尽可能多的人可以通过可能的解决方案得到帮助,但我想我把它做得太通用了。
这些答案可能对我没有帮助,但我希望其他人会。
样式仅适用于特定 class,它们不会被继承。不过,您可以这样做:
<Style TargetType="{x:Type myLibrary:SubView}">
<Setter Property="Opacity" Value="0.5"/>
</Style>
<Style TargetType="{x:Type localFolder:MyView}" BasedOn="{StaticResource {x:Type myLibrary:SubView}} />
如果 SubView
确实是一个自定义控件而不是 UserControl
并且具有在 Themes/Generic.xaml 中定义的默认模板,这实际上将按预期工作。
您可以使用以下示例代码自行确认。
Themes/Generic.xaml:
<Style TargetType="local:SubView">
<Setter Property="Opacity" Value="0.5" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:SubView}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
控件:
public class SubView : ContentControl
{
static SubView()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(SubView),
new FrameworkPropertyMetadata(typeof(SubView)));
}
}
public class MyView : SubView
{
}
用法:
<local:SubView>
<local:SubView.Content>
<TextBlock>content 1</TextBlock>
</local:SubView.Content>
</local:SubView>
<local:MyView>
<local:SubView.Content>
<TextBlock>content 2</TextBlock>
</local:SubView.Content>
</local:MyView>
来自 MSDN:"If you do need to create a new control, the simplest way is to create a class that derives from UserControl. Before you do so, consider that your control will not support templates and therefore will not support complex customization."
https://msdn.microsoft.com/en-us/library/system.windows.controls.usercontrol(v=vs.110).aspx
如何将样式应用于基于特定 CustomControl 的所有子项。
例子
自定义控件
public class SubView : UserControl
{ ... }
基于自定义控件的控件
public partial class MyView : SubView
{ ... }
XAML 为 MyView
<myLibrary:SubView
xmlns:myLibrary="....">
<Grid>
<!--Any content-->
</Grid>
</moduleChrome:SubView>
父级(此网格的子级是从代码运行时设置的)
<Grid>
<Grid.Resources>
<Style TargetType="myLibrary:SubView">
<Setter Property="MyCustomDependancy" Value="{binding to a shared MyCustomDependancy}"/>
</Style>
</Grid.Resources>
<myLibrary:SubView/> <!--This will have the shared MyCustomDependancy-->
<localFolder:MyView/> <!--But this will not be affected-->
</Grid>
如何让MyView受样式影响?
编辑
这段代码的起源是动态的并且相当复杂,但我试图让这个问题尽可能通用,以便尽可能多的人可以通过可能的解决方案得到帮助,但我想我把它做得太通用了。 这些答案可能对我没有帮助,但我希望其他人会。
样式仅适用于特定 class,它们不会被继承。不过,您可以这样做:
<Style TargetType="{x:Type myLibrary:SubView}">
<Setter Property="Opacity" Value="0.5"/>
</Style>
<Style TargetType="{x:Type localFolder:MyView}" BasedOn="{StaticResource {x:Type myLibrary:SubView}} />
如果 SubView
确实是一个自定义控件而不是 UserControl
并且具有在 Themes/Generic.xaml 中定义的默认模板,这实际上将按预期工作。
您可以使用以下示例代码自行确认。
Themes/Generic.xaml:
<Style TargetType="local:SubView">
<Setter Property="Opacity" Value="0.5" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:SubView}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
控件:
public class SubView : ContentControl
{
static SubView()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(SubView),
new FrameworkPropertyMetadata(typeof(SubView)));
}
}
public class MyView : SubView
{
}
用法:
<local:SubView>
<local:SubView.Content>
<TextBlock>content 1</TextBlock>
</local:SubView.Content>
</local:SubView>
<local:MyView>
<local:SubView.Content>
<TextBlock>content 2</TextBlock>
</local:SubView.Content>
</local:MyView>
来自 MSDN:"If you do need to create a new control, the simplest way is to create a class that derives from UserControl. Before you do so, consider that your control will not support templates and therefore will not support complex customization."
https://msdn.microsoft.com/en-us/library/system.windows.controls.usercontrol(v=vs.110).aspx