DataTemplate의 컨트롤을 얻는 좋은 방법은 무엇입니까? VisualTreeHelper를 사용하여 관심있는 컨트롤에서 Loaded 이벤트를 전달하는 기술을 사용하여 시각적 트리를 탐색하고 그 중 어느 것도 매우 우아하지 않습니다. DataTemplate 컨트롤에 액세스하는 좋은 방법은 무엇입니까?프로그래밍 방식으로 DataTemplate에서 컨트롤에 액세스
한 예로, ConverterParameter가 디자인 타임에 모르는 바인딩을 추가해야하며 ConverterParameters에 대한 바인딩이 지원되지 않으므로 코드에서 바인딩을 만들어야합니다. 이상 적으로는 데이터 형식의 컨트롤에 대한 Loaded 이벤트 핸들러가 아닌 다른 곳에서이 작업을 수행 할 수 있기를 원합니다.
실제로이 시나리오에서 이벤트 처리는 전혀 작동하지 않으며 OutOfMemoryException이 발생합니다.
generic.xaml을 :
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SilverlightTest">
<Style TargetType="local:TemplatedControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:TemplatedControl">
<ListBox ItemsSource="{TemplateBinding ListBoxItemsSource}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock x:Name="SomeTextBlock"
Loaded="SomeTextBlock_Loaded"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
templatedcontrol.cs : 여기 내 시도이다 당신은 그것을 처리하기 위해 DataTemplate을 다른 제어 할 수
public class TemplatedControl : Control
{
public object ListBoxItemsSource
{
get { return (object)GetValue(ListBoxItemsSourceProperty); }
set { SetValue(ListBoxItemsSourceProperty, value); }
}
public static readonly DependencyProperty ListBoxItemsSourceProperty =
DependencyProperty.Register
("ListBoxItemsSource", typeof(object),
typeof(TemplatedControl), new PropertyMetadata(null));
public TemplatedControl()
{
this.DefaultStyleKey = typeof(TemplatedControl);
}
public void SomeTextBlock_Loaded(object sender, RoutedEventArgs ea)
{
}
}
이것이 유용 할 수있는 시나리오를 포함시키기 위해 약간 확장 할 수 있습니까? –
질문에 추가 중 ... –