2012-10-25 12 views
2

我有一個奇怪的情況,我在WPF窗口中與轉換器綁定。偶爾,這個窗口的內容從該窗口中刪除和插入標籤式窗口,如下所示:綁定在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以避免StaticReferences的轉換器。我的轉換器看起來像這樣

[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的工作。可見性更新並顯示正常。

回答

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

,它同時適用於用戶控件和stackpanel。 希望這可能有幫助。

+0

我的屬性看起來像這樣,並且使用標記是我之前一直在做的事情(除非我使用的是在合併字典中定義的樣式),但不幸的是,它不起作用。當窗口內容被移除並被插入到帶有newTab.Style =(Style)Resources [「CorsairTab」]的選項卡中時,樣式將被拋出並被替換。 – pfoley

相關問題