私は以下を持っていますUserControl
:
<UserControl x:Class="MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<SolidColorBrush x:Key="ColorKey" Color="Orange"/>
</UserControl.Resources>
<Grid Background="{StaticResource ColorKey}">
</Grid>
</UserControl>
そして、私はそれを次のように使用します:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:OverrideResource"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<SolidColorBrush x:Key="OtherColorKey" Color="Blue"/>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<local:MyControl Grid.Row="0">
<local:MyControl.Resources>
<SolidColorBrush x:Key="ColorKey" Color="Red"/>
</local:MyControl.Resources>
</local:MyControl>
<Grid Grid.Row="1">
<Grid.Resources>
<SolidColorBrush x:Key="OtherColorKey" Color="Green"/>
</Grid.Resources>
<Grid Background="{StaticResource OtherColorKey}"/>
</Grid>
</Grid>
</Window>
リソースのオーバーライドは、OtherColorKey
期待どおりに機能します。グリッドは緑Background
です。しかし、 (私の例では)Resource
内で使用されているa をオーバーライドしたいと思います。しかし、私は例外を取得しています:UserControl
ColorKey
アイテムは既に追加されています。辞書のキー: 'ColorKey' 追加されるキー: 'ColorKey'
これは単純化された例にすぎません。実際には、もう少し複雑なタスクに必要です。たとえば、DevExpress は、コントロールをカスタマイズするために同様のメカニズムを使用していることを知っています (ただし、キーとして文字列ではなく、 から派生したオブジェクトを使用しますResourceKey
)。しかし、そのようなことを自分で実装するための簡単な実例を見つけることができません。
ご協力いただきありがとうございます。