자원 내에서 조상에게 구속력이 있습니다
-
20-09-2019 - |
문제
어떻게 a에 묶을 수 있습니까? UserControl
그 내의 재산 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
Code-Behind의 리소스.
.xaml
<local:SomeType x:Key="SomeKey" SomeProperty="{Binding ... }" />
.xaml.cs
public SomeControl()
{
InitializeComponent();
((SomeType)this.Resources["SomeKey"]).DataContext = this;
}
다른 팁
FindAncestor를 사용할 때 대상 요소는 소스의 후손 (논리적 또는 시각적)이어야합니다. 객체는 시각적 또는 논리적 트리에 나타나지 않으며 단순히 리소스에 있습니다. 따라서 당신은 당신의 대상에 대한 findancestor와 함께 친척 소스를 사용할 수 없습니다.
그래도 바인딩에서 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>
세트 X : 공유= "false", 이것은 각 사용마다 자원을 복제하고 요소의 자식으로 만들어 바인딩 사용을 가능하게합니다.
<local:SomeClass
x:Key="SomeClass"
x:Shared="False"
DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
나는 당신이 찾고있는 것이 상속 된 DataContext에 바인딩하는 {binding} 일 뿐이라고 생각합니다. 예를 들어, 약간 이상한 것은 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)을 만들고 첨부 된 동작에 대한 리소스 키를 지정하는 것입니다. 동작은 리소스를 조회하고 (로드 된 이벤트를 연결 해야하는 경우 첨부 할 수 있는지 확실하지 않음) 및 데이터 컨텍스트를 전송합니다.
리소스를 시각적 트리에 추가하면 데이터 컨텍스트를 상속해야합니다. 그러나 ...보세요 요소 스파이 필요한 것을 할 수 있습니다.