2

我有一个奇怪的情况,我在 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 时,一切正常。能见度更新并显示正常。

4

1 回答 1

0

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

         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>

更新绑定

Visibility="{Binding Path=IsEye,Converter={StaticResource BoolToVisibilityConverter}}"

它适用于用户控件和堆栈面板。希望这可能会有所帮助。

于 2012-10-25T19:24:18.463 回答