I’m trying to Bind the foreground of a Label inside a DataTemplate to an Ancestor, this is the extract:
ViewModel Code
public class DeviceOverview
{
public string DeviceName { get; set; }
public string DeviceState { get; set; }
}
public ObservableCollection<DeviceOverview> DeviceOverviewList { get; set; }
This is the xaml:
<ItemsControl ItemsSource="{Binding DeviceOverviewList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<DockPanel Margin="20,5,5,5">
<Image Source="{Binding DeviceState, Converter={StaticResource ConvertStateToLed}}" Width="30"/>
<Label Content="{Binding DeviceName}" Width="220" Margin="-60,0,0,0" Foreground="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.FontColor}"/>
</DockPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
The Window control has its own DataContext with his ViewModel and its property FontColor, and everything works. Still, i get the following error:
The FontColor property was not found in the DeviceOverview object.
Why do I get this error? And how do I can get rid of it?
Thanks