wpf - 是否可以更改在 XAML 中使用 StaticResource 元素实例化的资源的属性?
问题描述
使用这段 XAML:
<Window.Resources>
<Rectangle x:Key="rectangle" x:Shared="False" Width="20" Height="8" Fill="Red" />
</Window.Resources>
<StaticResource x:Name="r1" ResourceKey="rectangle" />
<StaticResource x:Name="r2" ResourceKey="rectangle" />
可以通过代码独立地为每个实例分配一个值来表示 Margin 属性:
r1.Margin = 2;
r2.Margin = 5;
是否可以直接在 XAML 中执行?我试过了:
<StaticResource ResourceKey="rectangle" Margin="3"/>
但保证金不是StaticResource
...的财产
在 XY 问题传感器触发后改写(适当地)!
我想绘制具有完全相同属性的矩形,除了一个,例如边距或颜色,以便能够集中更改共享属性并仍然能够在 XAML 中提供特定属性。我可以在尝试中使用资源吗?
按照评论的建议添加我的确切需求和代码
我的确切需要是显示为某个矩形设置不同属性的效果,即更改Rectangle.RenderTransformOrigin
和Rectangle.RenderTransform
比较效果。确实是为了学习 WPF,而不是为了生产应用程序。目前,我使用样式(rotated
),因为我无法使用资源(这是我上面问题的原因)。
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Test"
mc:Ignorable="d"
Title="Transform Center" Height="400" Width="600">
<Window.Resources>
<Style x:Key="title" TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Bottom" />
<Setter Property="Margin" Value="0,0,0,10" />
</Style>
<Style x:Key="rotated" TargetType="Rectangle">
<Setter Property="Width" Value="201" />
<Setter Property="Height" Value="81" />
<Setter Property="Fill" Value="CadetBlue"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
</Style>
<Style x:Key="fixed" TargetType="Rectangle">
<Setter Property="Width" Value="30" />
<Setter Property="Height" Value="30" />
<Setter Property="Fill" Value="Indigo" />
<Setter Property="HorizontalAlignment" Value="Left"/>
</Style>
</Window.Resources>
<Grid >
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<StackPanel Grid.Row="0" Grid.Column="0" Background="Beige" Margin="5">
<Rectangle Style="{StaticResource fixed}" />
<Rectangle Style="{StaticResource rotated}" />
</StackPanel>
<TextBlock Grid.Row="0" Grid.Column="0"
Style="{StaticResource title}" Text="No rotation" />
<StackPanel Grid.Row="0" Grid.Column="1" Background="Beige" Margin="5">
<Rectangle Style="{StaticResource fixed}" />
<Rectangle Style="{StaticResource rotated}">
<Rectangle.RenderTransformOrigin>.5,.5</Rectangle.RenderTransformOrigin>
<Rectangle.RenderTransform>
<RotateTransform Angle="20" />
</Rectangle.RenderTransform>
</Rectangle>
</StackPanel>
<TextBlock Grid.Row="0" Grid.Column="1"
Style="{StaticResource title}"
Text="RenderTransformOrigin" />
<StackPanel Grid.Row="1" Grid.Column="0" Background="Beige" Margin="5">
<Rectangle Style="{StaticResource fixed}" />
<Rectangle Style="{StaticResource rotated}">
<Rectangle.RenderTransform>
<RotateTransform Angle="20" CenterX="100" CenterY="40" />
</Rectangle.RenderTransform>
</Rectangle>
</StackPanel>
<TextBlock Grid.Row="1" Grid.Column="0"
Style="{StaticResource title}"
Text="RotateTransform Center" />
<!-- The center coordinates relative to the Rectangle are the sum
of both center coordinates, i.e. .5 + .5 = 1 (bottom-right corner) -->
<StackPanel Grid.Row="1" Grid.Column="1" Background="Beige" Margin="5">
<Rectangle Style="{StaticResource fixed}" />
<Rectangle Style="{StaticResource rotated}">
<Rectangle.RenderTransformOrigin>.5,.5</Rectangle.RenderTransformOrigin>
<Rectangle.RenderTransform>
<RotateTransform Angle="20" CenterX="100" CenterY="40" />
</Rectangle.RenderTransform>
</Rectangle>
</StackPanel>
<TextBlock Grid.Row="1" Grid.Column="1"
Style="{StaticResource title}"
Text="Both" />
</Grid>
</Window>
解决方案
XAML 是否允许在实例化资源时更改其属性
<StaticResource>
简短的回答:没有。
StaticResource
标记扩展只是简单地基于键引用资源。它不能更改已解析资源的任何属性。您必须设置已解析资源本身的属性,例如将资源的目标属性转换为Rectangle
.
推荐阅读
- c# - 如果属性已存在于同一数组.net 中的另一个对象中,如何不显示对象
- vue.js - 在故事书中使用 SASS(缩进语法)
- javascript - 如何使用 `x.some(word=>y.includes(word))` 情况切换()变量
- javascript - How do I set a different range for line chart embedded in a bar chart using d3.js
- numpy - 导入numpy时DLL加载失败_multiarray_unmath
- c++ - 错误模块名称:ntdll.dll,版本:6.3.9600.19678 异常代码:0xc0000005
- django - Django 3:AttributeError:'AdminSite'对象没有属性'Register'
- typescript - Nestjs如何在没有中间链接的情况下重定向
- scala - 如何根据 spark/scala 中的每个索引位置查找每个值的总和
- kubernetes - 按 apiVersion 列出 Kubernetes 资源