2011-06-26 45 views
0

我有幾個堆棧面板,我只想顯示一個被選中(從另一個組合框)。 正如你所看到的,在stackpanel內部有一個組合框,它將選定的值綁定到一個對象。防止從隱藏控件綁定

我的問題是,每個面板更新和重寫對象,即使是隱藏的!
是否可以防止隱藏對象的綁定?

<StackPanel x:Name="pnl_1" Orientation="Horizontal" 
      Visibility="{Binding SelectedItem.Name,ElementName=comboProp, Mode=OneWay, ConverterParameter=pnl_1, Converter={StaticResource PanelVisibilityConverter},FallbackValue=Collapsed}"> 
      <ComboBox Height="23" Width="90"         
         ItemsSource="{Binding Source={StaticResource Source1}}"                 
         SelectedValue="{Binding Path=Data.Operand, Converter={StaticResource Converter1}}"> 
      </ComboBox> 

</StackPanel> 


<StackPanel x:Name="pnl_2" Orientation="Horizontal" 
      Visibility="{Binding SelectedItem.Name,ElementName=comboProp, Mode=OneWay, ConverterParameter=pnl_2, Converter={StaticResource PanelVisibilityConverter},FallbackValue=Collapsed}"> 
      <ComboBox Height="23" Width="90"         
         ItemsSource="{Binding Source={StaticResource Source2}}"                 
         SelectedValue="{Binding Path=Data.Operand, Converter={StaticResource Converter2}}"> 
      </ComboBox> 

</StackPanel> 
+0

綁定是綁定;他們不依賴於知名度。綁定到不可見的元素實際上非常有用。無論如何,你只需要另闢蹊徑。 –

回答

1

將數據綁定到不可見的UIElement沒有任何問題,只要它不會導致性能問題。

如果要根據可見性設置數據綁定,則必須在源代碼中執行此操作;你不能在XAML中完成。

下面是一個例子如何創建動態綁定:

Binding myBinding = new Binding("Data.Operand"); 
myBinding.Source = myItemsSource; 
BindingOperations.SetBinding(myComboBox, ComboBox.SelectedValueProperty, myBinding); 

並清除綁定,您將使用

BindingOperations.ClearBinding(myComboBox, ComboBox.SelectedValueProperty); 
0

您可以用DataTriggers綁定條件,這裏有一個例子列表框,如果其周圍的邊框可見,則只設置其ItemsSource

<ListBox Height="100" ScrollViewer.HorizontalScrollBarVisibility="Auto" 
     ScrollViewer.VerticalScrollBarVisibility="Auto"> 
    <ListBox.Style> 
     <Style TargetType="{x:Type ListBox}"> 
      <Style.Triggers> 
       <DataTrigger 
         Binding="{Binding Visibility, RelativeSource={RelativeSource AncestorType=Border}}" 
         Value="Visible"> 
        <Setter Property="ItemsSource" 
          Value="{Binding Source={x:Static local:App.Log}, Path=Buffer}" /> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </ListBox.Style> 
</ListBox> 
0

是的,那是可能的。 我是在一個類和它的子類的情況下做到的。 我的例子可能不像你的情況,但原則可能很適用。

我有一個A類和兩個A的子類,名爲AB和AC。 AC和AB都具有超出A的額外屬性。

然後是包含As,AB和AC的ObservableCollection。 我必須爲集合元素的單個成員顯示集合的DataGrid和StackPanel。

對於AB和AC類的額外成員,我在XAML中定義了兩個StaticResource StackPanel。 (I總是要顯示基類A的成員,因此,這些成員是該結構之外。)

<StackPanel x:Key="AB"...../> 
<StackPanel x:Key="AC"...../> 
<StackPanel x:Key="Nothing"...../> 

這些StackPanels,因爲它們是資源,將不能正常運行時綁定:只有當並且如果它們必然會!! 現在我定義了一個ContentControl中,綁定到一個命名屬性「SubClassPanel」:

public StackPanel SubClassPanel {get; set; etc...} 

現在我可以編程設置SubClassPanel到eihter的AB資源或AC資源或空,假的StackPanel的資源,如果你只是想顯示基類。 這裏是子類StackPanels的插入點的XAML:

<ContentControl Content="{Binding SubClassPanel, 
      Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/> 

變化完成後,在我的情況下,使用DataGrid事件「的SelectionChanged」。這裏是事件管理器:

public void ChangeSection(object sender, SelectionChangedEventArgs e) 
    { 
     // sender is the datagrid 
     if (sender == null) 
      return; 
     // So, SelectedItem can be of class A, AB or AC: 
     var s = (sender as DataGrid).SelectedItem; 

     if (s is AB) 
      (s as AB).SubClassPanel = this.FindResource("AB") as StackPanel; 
     else 
     { 
      if (s is AC) 
       (s as AC).SubClassPanel = this.FindResource("AC") as StackPanel; 
      else 
      { 
       // or show just the base class members: 
       (s).SubClassPanel = this.FindResource("Nothing") as StackPanel; 
      } 
     } 
    }