結合する祖先からResourceDictionary
-
20-09-2019 - |
質問
たいのですが結合 UserControl
's性からその ResourceDictionary
?したいオブジェクトを宣言しっ資源の DataContext
としての UserControl
に含まれる:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Some.Namespace"
DataContext="{Binding Path=ViewModel, RelativeSource={RelativeSource Self}}">
<UserControl.Resources>
<local:SomeClass
x:Key="SomeClass"
DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
</UserControl.Resources>
</UserControl>
実行時のエラー:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='UserControl', AncestorLevel='1''. BindingExpression:Path=DataContext; DataItem=null; target element is 'SomeClass' (Name=''); target property is 'DataContext' (type 'Object')
解決
私の回避策をセットにした DataContext
のリソースのコードです。
.ー
<local:SomeType x:Key="SomeKey" SomeProperty="{Binding ... }" />
.ー.cs
public SomeControl()
{
InitializeComponent();
((SomeType)this.Resources["SomeKey"]).DataContext = this;
}
他のヒント
、ターゲット要素は、ソースの(論理的または視覚的のいずれか)の子孫である必要があります。あなたのオブジェクトは、それが資源に単純ですが、どちらかの視覚的または論理ツリーには表示されません。つまり、あなたのオブジェクトに対してFindAncestorでRelativeSourceを使用することはできません。
あなたはしかし、あなたのバインディングでのElementNameを使用することができます。このような何か作業をする必要があります:
<UserControl x:Name="userControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Some.Namespace"
DataContext="{Binding Path=ViewModel, RelativeSource={RelativeSource Self}}">
<UserControl.Resources>
<local:SomeClass
x:Key="SomeClass"
DataContext="{Binding Path=DataContext, ElementName=userControl}" />
</UserControl.Resources>
</UserControl>
×セット:共有する = "false" に、これは上のリソースのクローンを作成しますそれぞれが使用して、バインディングの使用を可能にする、それあなたの要素の子作ります。
<local:SomeClass
x:Key="SomeClass"
x:Shared="False"
DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
私はあなたが継承されたのDataContextに結合するだけで、{}バインドされて探しているものだと思います。
:ここでの例では、あなたがのDataContextへの結合を介して色をつかむことができますどのように少し奇妙に示しても、です<Window x:Class="AncestorBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<SolidColorBrush x:Key="MyBrush" Color="Blue" />
</Window.Resources>
<StackPanel>
<Button DataContext="{Binding Source={StaticResource MyBrush}}" Content="My Button">
<Button.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="{Binding}" />
</Style>
</Button.Resources>
</Button>
</StackPanel>
</Window>
私は何だろうと、ユーザーコントロールの上に取り付けた行動(ContextualizeResourceBehavior)を作成し、及びその添付行動上のリソースキーを指定することです。動作は、リソース(あなたがLoadedイベントをフックする必要がありますない場合は、接続時にそれを行うことができるだろうかわからない)を検索し、データコンテキストを転送します。
あなたがビジュアルツリーにあなたのリソースを追加するとき、それはデータコンテキストを継承する必要があります。しかし... 要素のスパイを見て>それはちょうどあなたが必要なものを行う可能性があります。