【问题标题】:Binding in WPF window only works occasionally when removing content from windowWPF 窗口中的绑定仅在从窗口中删除内容时偶尔起作用
【发布时间】:2012-10-16 00:02:25
【问题描述】:

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

【问题讨论】:

    标签: wpf xaml binding user-controls ivalueconverter


    【解决方案1】:

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

             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}}"
    

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

    【讨论】:

    • 我的属性已经看起来像那样了,并且使用那个 标记是我以前一直在做的(除了我使用的是合并字典中定义的样式),不幸的是,没有工作。当使用 newTab.Style = (Style)Resources["CorsairTab"]; 删除窗口内容并将其插入选项卡时,样式会被丢弃并替换
    猜你喜欢
    • 2011-01-21
    • 2013-03-19
    • 2016-03-23
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 2015-12-31
    • 1970-01-01
    相关资源
    最近更新 更多