2013-03-24 65 views
0

我有一個組合框,我想動態地將MaxDropDownHeight屬性綁定到第二行高度。WPF將MaxDropDownHeight屬性綁定到網格行高度

這裏的XAML:

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*" /> 
     <RowDefinition Height="6*" /> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*" /> 
     <ColumnDefinition Width="*" /> 
    </Grid.ColumnDefinitions> 

    <ComboBox MaxDropDownHeight=""> 

    </ComboBox> 
</Grid> 

我怎麼能這樣做?

回答

1

綁定到的Grid秒行,你可以通過兩種方式實現:

第一:通過RelativeSource比南:

<ComboBox DropDownOpened="ComboBox_DropDownOpened" 
      MaxDropDownHeight="{Binding Path=RowDefinitions[1].ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}, UpdateSourceTrigger=PropertyChanged}">  
</ComboBox> 

二:通過ElementName結合(在這種情況下,你必須在電網Name="RootLayout"集) :

<ComboBox DropDownOpened="ComboBox_DropDownOpened" 
      MaxDropDownHeight="{Binding ElementName=RootLayout, Path=RowDefinitions[1].ActualHeight, UpdateSourceTrigger=PropertyChanged}">    
</ComboBox> 

DropDownOpened事件處理程序,你應該更新的值通過使用BindingExpression類。

private void ComboBox_DropDownOpened(object sender, EventArgs e) 
{ 
    ComboBox cb = sender as ComboBox; 
    BindingExpression be = cb.GetBindingExpression(ComboBox.MaxDropDownHeightProperty); 
    be.UpdateTarget(); 
} 
相關問題