WPF窗口中的绑定仅在从窗口中删除内容时偶尔起作用

WPF窗口中的绑定仅在从窗口中删除内容时偶尔起作用,wpf,xaml,binding,user-controls,ivalueconverter,Wpf,Xaml,Binding,User Controls,Ivalueconverter,我有一个奇怪的情况,我在WPF窗口中绑定了一个转换器。偶尔,此窗口的内容会从所述窗口中删除,并插入到选项卡式窗口中,如下所示: public void AddNewTab(Window wpfWindow, String tabTitle, OnFocusHandler onFocusHandler) { //Unhook window contents object content= wpfWindow.Content;

我有一个奇怪的情况,我在WPF窗口中绑定了一个转换器。偶尔,此窗口的内容会从所述窗口中删除,并插入到选项卡式窗口中,如下所示:

        public void AddNewTab(Window wpfWindow, String tabTitle, OnFocusHandler onFocusHandler)
    {
        //Unhook window contents
        object content= wpfWindow.Content;
        wpfWindow.Content = null;

        //Create a new tab
        TabItem newTab = new TabItem();
        newTab.Header = title;

        newTab.Style = (Style)Resources["CorsairTab"];

        //newTab.Foreground = Brushes.White;
        newTab.Background = Brushes.Transparent;
        newTab.Content = content;

        //Add it
        TabControl.Items.Add(newTab);

        //Tie handler if it exists
        if (onFocusHandler != null)
            _listOnTabSelectedEventHandlers.Add(onFocusHandler);
        else
            _listOnTabSelectedEventHandlers.Add(null);

        //If this is the first tab, make it the opened one
        if(TabControl.Items.Count == 1)
            TabControl.SelectedIndex = 0;
    }
这一切都很好,但是当所讨论的内容剥离窗口与转换器绑定时,就会出现问题。我编写了一个从MarkupExtension继承的转换器,以避免静态引用。我的转换器看起来像这样

[ValueConversion(typeof(bool), typeof(Visibility))]
public class BoolToVisibilityConverter : MarkupExtension, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool isWorking = (bool)value;
        if (isWorking)
        {
            return Visibility.Visible;
        }
        else
        {
            return Visibility.Collapsed;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }

}
引用它的XAML如下所示:

<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Grid.Column="1" Grid.Row="1" Visibility="{Binding Path=IsEye, Converter={inf:BoolToVisibilityConverter}}">
    <TextBlock Text="Working Eye" VerticalAlignment="Top" Foreground="White" Margin="5,5,0,0"/>
    <Button Content="Approve All" Command="{Binding ApproveAllTrades}" 
                        Margin="5,0,0,0" Width="auto" Height="auto" VerticalAlignment="Top" Background="#DCDCDC"/>
 </StackPanel>
 <views:OrdersWorkingEyeView Loaded="EyeOrders_Loaded" Grid.Column="1" Grid.Row="2" Visibility="{Binding Path=IsEye, Converter={inf:BoolToVisibilityConverter}}"/>

暂时忽略BoolToVisibility已经是一个定义的东西,当我剥离窗口的内容并加载这两个特定控件(一个由我定义,另一个是堆栈面板)时,ProvideValue处设置的断点会命中两次(每个控件一次),但对于我的自定义控件,Convert只调用一次。结果是自定义控件具有适当的可见性,但堆栈面板没有。我非常确定绑定本身作为两个控件上绑定的路径是相同的,并且适用于自定义控件。我无法找出导致其中一个发生转换而另一个不发生转换的区别(或者,可能是为什么StackPanel不能正确绑定)。帮忙

编辑


值得一提的是,当我不将内容从窗口中剥离并放入新的TabItem时,一切都正常。可视性更新并显示良好。

我尝试了您的示例,并进行了以下更改以使其正常工作

         private bool _isEye= true;

        public bool IsEye
        {
            get { return _isEye; }
            set { _isEye = value;
            NotifyFropertyChanged("IsEye");
            }
        }
定义资源

<Window.Resources>
    <!-- local is your namespace--> 
    <local:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
</Window.Resources>
它同时适用于usercontrol和stackpanel。
希望这能有所帮助。

我的属性看起来已经是这样了,使用该标记是我以前一直在做的事情(除了使用合并词典中定义的样式),但不幸的是,这不起作用。当删除窗口内容并使用newTab将其插入选项卡时,样式将被抛出并替换。style=(style)Resources[“CorsairTab”];
Visibility="{Binding Path=IsEye,Converter={StaticResource BoolToVisibilityConverter}}"