Understanding WPF data binding scope
I have a custom control embedded within a TabItem of the TabControl. TabControl is Content of main Usercontrol as follows:
<UserControl x:Class="ControlsLibrary.wpf.Alerts.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<Grid>
<TabControl DataContext="{Binding Filter}">
<TabItem>
<mc:CustomControl DataContext="{Binding Options}"
</TabItem>
</TabControl>
</Grid>
</UserControl>
What I understand is my custom control will inherit DataContext of TabControl, therefore DataContext of my custom control should be set to the "Options" property of Filter object which is DataContext of TabControl. However, Visual Studio's output window indicates that it is looking for Options in DataContext of parent UserControl.
Even if I use ElementName property of the Binding class, I still cannot get DataContext of my custom control to change, even though property "Filter" of data item implements INotifyPropertyChan开发者_如何学Cged.
What did I miss?
TIA.
I saw a similar problem (but not a Tab control issue, so my apologese if this doesnt work) to this a while ago and what I needed to so was to set the CustomControl
as the control template of the TabItem
.
<TabControl DataContext="{Binding Filter}">
<TabItem>
<TabItem.Template>
<ControlTemplate>
<mc:CustomControl DataContext="{Binding Options}" />
</ControlTemplate>
</TabItem.Template>
</TabItem>
</TabControl>
which actually has an impact of the logic and visual positioning of the CustomControl
in the inheritance hierarchy.
Try this an let me know if it works
HTH
精彩评论