开发者

Understanding WPF data binding scope

开发者 https://www.devze.com 2023-01-20 05:32 出处:网络
I have a custom control embedded within a TabItem of the TabControl.TabControl is Content of main Usercontrol as follows:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号