c# – wpf为命名元素创建样式
发布时间:2020-12-15 22:28:10 所属栏目:百科 来源:网络整理
导读:是否可以在不编辑元素的情况下为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
是否可以在不编辑元素的情况下为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> UPD: 如果我编辑元素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> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |