简体   繁体   English

WPF - 绑定不起作用

[英]WPF — Binding Not Working

I'm having a rather frustrating problem here... I have a WPF Page that contains a TabControl, and the content of the various TabItems is another WPF Page (hosted in a Frame because Page can only have Frame or Window as a parent). 我在这里有一个相当令人沮丧的问题...我有一个包含TabControl的WPF页面,各种TabItems的内容是另一个WPF页面(托管在一个Frame中,因为Page只能将Frame或Window作为父项) 。 Even though the FlowCalibrationSummaryView is being displayed, everything on it is empty because the data binding of the SummaryViewModel is not working for some reason. 即使正在显示FlowCalibrationSummaryView,其上的所有内容都是空的,因为SummaryViewModel的数据绑定由于某种原因不起作用。 Here's part of the XAML: 这是XAML的一部分:

<TabControl Grid.Row="0">
    <TabItem Header="Current Calibration">
        <TabItem.Content>
            <Frame>
                <Frame.Content>
                    <view:FlowCalibrationSummaryView DataContext="{Binding SummaryViewModel}"/>
                </Frame.Content>
            </Frame>
        </TabItem.Content>
    </TabItem>
</TabControl>

I have a break point on the get of SummaryViewModel, and it is only getting hit by the code that is constructing it in the parent view model. 我在SummaryViewModel的get上有一个断点,它只会被在父视图模型中构造它的代码所击中。 Here's the property being bound to: 这是被绑定的财产:

    public const string SummaryViewModelPropertyName = "SummaryViewModel";
    private FlowCalibrationSummaryViewModel _SummaryViewModel;
    public FlowCalibrationSummaryViewModel SummaryViewModel
    {
        get { return _SummaryViewModel; }
        set
        {
            if (_SummaryViewModel == value)
                return;

            _SummaryViewModel = value;
            RaisePropertyChanged(SummaryViewModelPropertyName);
        }
    }

At this point I'm completely stumped, I cannot for the life of me figure out why this binding is not working. 在这一点上,我完全难倒,我不能为我的生活弄清楚为什么这个绑定不起作用。 Any help would be much appreciated. 任何帮助将非常感激。

Update: It definitely has something to do with it being in a Frame. 更新:它肯定与它在一个框架中有关。 I tried changing the FlowCalibrationSummaryView to a UserControl instead of a Page to see if that helped, and it didn't, then I tried taking it out of the Frame it was wrapped in and that worked. 我尝试将FlowCalibrationSummaryView更改为UserControl而不是Page,以查看是否有帮助,但事实并非如此,然后我尝试将其从包含的框架中取出并且有效。 All of the views in my project are done as Pages so for consistency's sake I'd prefer this to be a Page, but then I have to host it in a Frame. 我的项目中的所有视图都是作为Pages完成的,所以为了保持一致性,我更喜欢这是一个Page,但是我必须在一个Frame中托管它。 Is there something I'm missing here, or is the proper usage to do it as a UserControl? 有没有我在这里缺少的东西,或者作为UserControl正确使用它?

I would take a look at the answer provided in this question . 我想看看这个问题中提供的答案。 It seems to provide a specific explanation of the behavior of Frame in this case. 在这种情况下,它似乎提供了Frame行为的具体解释。

Unless there is a specific reason you have chosen Frame over UserControl , you would be better off re-implementing this (and any other sub-view) as UserControl instead. 除非有特定的原因你选择了Frame over UserControl ,否则你最好将这个(以及任何其他子视图)重新实现为UserControl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM