내 응용 프로그램에는 색상 리소스가 있습니다. xaml에서 동적 리소스로 해당 색을 사용하는 요소가 하나 있습니다.WPF에서 코드 뒤에 리소스 참조를 복제하려면 어떻게해야합니까?
<Window x:Class="ResourcePlay.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="425">
<Window.Resources>
<Color x:Key="MyColor">Red</Color>
</Window.Resources>
<Grid>
<Rectangle VerticalAlignment="Top" Width="80" Height="80" Margin="10">
<Rectangle.Fill>
<SolidColorBrush x:Name="TopBrush" Color="{DynamicResource MyColor}"/>
</Rectangle.Fill>
</Rectangle>
<Rectangle VerticalAlignment="Bottom" Width="80" Height="80" Margin="10">
<Rectangle.Fill>
<SolidColorBrush x:Name="BottomBrush"/>
</Rectangle.Fill>
</Rectangle>
</Grid>
</Window>
코드에서이 리소스 참조를 복제하려고합니다.
using System.Windows;
using System.Windows.Media;
namespace ResourcePlay {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
// I want to copy the resource reference, not the color.
BottomBrush.Color = TopBrush.Color;
// I'd really rather do something like this.
var reference = TopBrush.GetResourceReference(SolidColorBrush.ColorProperty);
BottomBrush.SetResourceReference(reference);
// I want this to change the colors of both elements
Resources["MyColor"] = Colors.Green;
}
}
}
그러나 SetResourceReference는 FrameworkElements 또는 FrameworkContentElements에서만 작동합니다. SolidColorBrush는 단지 Freezable입니다. 또한, 난 어떻게 코드 뒤에 리소스 참조 얻을 모르겠어요.
두 색상이 동시에 변경되도록 WPF에서이 작업을 수행 할 수 있습니까? 제 실제 응용 프로그램에서는 문제가 아주 간단하지 않으므로 xaml에 두 번째 DynamicResource를 추가 할 수는 없습니다.
'SolidColorBrush'를 리소스 자체로 선언 한 다음 원하는 요소의 'Fill'속성에 사용하는 이유가 무엇인지 구체적으로 설명해주십시오. –
@PeterDuniho 여기 SolidColorBrush는 내 응용 프로그램에서 사용하는 사용자 정의 Freezable 서브 클래스의 프록시입니다. 실제 개체는 종속성 주입을 위해 매개 변수화되어 있으므로 xaml에서 개체를 만들 수 없습니다. –
내 정확한 질문이기 때문에 왜 누군가가 문제를 downvote 모르겠다. 이 스레드는 나를위한 해결책을 제공했습니다. – TravisWhidden