2013-03-24 64 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(); 
} 
相关问题