ListView Item의 DataTemplate의 TextBlock 및 Rectangle의 색을 코드로 직접 정하고 싶었음
<ListView ItemsSource="{Binding FileInfos}"
SelectedItem="{Binding SelectedFileInfo}"
Style="{DynamicResource FileLoadListViewStyle}"
ItemContainerStyle="{StaticResource FileItemStyle}"
x:Name="xFileInfoListView">
<ListView.ItemsPanel>
<ItemsPanelTemplate >
<WrapPanel/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<DockPanel Loaded="FrameworkElement_OnLoaded">
<TextBlock x:Name="xTextBlockInDataTemplate" DockPanel.Dock="Bottom" Text="{Binding Name}" Style="{DynamicResource FileViewerListItemTextBlockStyle}" />
<Rectangle x:Name="xRectangleInDataTemplate" Style="{DynamicResource FileViewerRectangleStyle}">
<Rectangle.OpacityMask>
<VisualBrush Stretch="Fill" Visual="{DynamicResource BinaryFile}"/>
</Rectangle.OpacityMask>
</Rectangle>
</DockPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
DataTemplate의 요소의 이름을 주어도 직접적으로 접근을 할 수 없음.
해결방법
VisualTreeHelper를 사용하여 해결
private childItem FindVisualChild<childItem>(DependencyObject obj)
where childItem : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is childItem)
{
return (childItem)child;
}
else
{
childItem childOfChild = FindVisualChild<childItem>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
public void DataTemplateElementsColorChange(ColorBuffer buffer)
{
var xListView = _FileInfoComponentV.xFileInfoListView;
foreach (var item in xListView.Items.SourceCollection)
{
System.Windows.Controls.ListViewItem myListViewItems =
(System.Windows.Controls.ListViewItem)(xListView.ItemContainerGenerator.ContainerFromItem(item));
if (myListViewItems != null)
{
ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListViewItems);
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
DataTemplate myDataTemplate2 = myListViewItems.ContentTemplate;
TextBlock myTextBlock = (TextBlock)myDataTemplate.FindName("xTextBlockInDataTemplate", myContentPresenter);
Rectangle myRectangle = (Rectangle)myDataTemplate.FindName("xRectangleInDataTemplate", myContentPresenter);
myTextBlock.Foreground =
new SolidColorBrush(buffer.TextForegroundColor);
myRectangle.Fill = new SolidColorBrush(buffer.TextForegroundColor);
}
}
}
여기서 요소의 제일 안쪽에 있는 ContentPresenter에 접근하여 바꿀 수 있었다.
WPF 아주 어렵다.