是否可以在不编辑元素的情况下为xaml元素添加样式?
例如:
xaml元素:
<Grid> <Grid x:Name="A">content A</Grid> <Grid x:Name="B">content B</Grid> </Grid>
和风格:
<Style x:Key="StyleForA" TargetName="A" targettype="{x:Type Grid}" > <Setter Property="Background" Value="Red"/> </Style> <Style x:Key="StyleForB" TargetName="B" targettype="{x:Type Grid}" > <Setter Property="Background" Value="Green"/> </Style>
如果我编辑元素Style =“{StaticResources StyleForA}”,我必须编辑所有样式.
所以我需要创建影响命名元素的本地样式.
解决方法
本地你不能.
但是,如果不接触您的XAML,您可以使用代码轻松完成:
Grd1.Style = (Style) this.Resources["GridKey"];
使用Blend行为的纯XAML方法,
<Grid x:Name="Grd1"> <i:Interaction.Triggers> <i:EventTrigger EventName="Loaded"> <ic:ChangePropertyAction PropertyName="Style" Value="{DynamicResource GridKey}"/> </i:EventTrigger> </i:Interaction.Triggers> </Grid>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。