5

我有以下 xaml:

<Grid KeyboardNavigation.TabNavigation="Local" SnapsToDevicePixels="True">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Border Background="Transparent" BorderThickness="0,0,0,2"  BorderBrush="{StaticResource TabPanelBorderBrush}">
            <DockPanel LastChildFill="True">
                <Button x:Name="LeftButton" Content="3" DockPanel.Dock="Left"   Style="{DynamicResource TabControlButton}"></Button>
                <StackPanel Orientation="Horizontal" DockPanel.Dock="Right">
                    <Button x:Name="RightButton" Content="4" Style="{DynamicResource TabControlButton}"></Button>
                    <Button x:Name="TabItemsList"  Content="L" FontFamily="Segoe UI" Style="{DynamicResource TabControlButton}"></Button>
                    <Button x:Name="AddTabItem" Content="+" FontFamily="Segoe UI" Style="{DynamicResource TabControlButton}"></Button>
                </StackPanel>
                <ScrollViewer x:Name="ScrollViewer" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Hidden">
                    <TabPanel x:Name="HeaderPanel" IsItemsHost="True" Panel.ZIndex="1" KeyboardNavigation.TabIndex="1"/>
                </ScrollViewer>
            </DockPanel>
        </Border>
        <Border Grid.Row="1" Background="{StaticResource TabControlBackground}"/>
        <ContentPresenter Grid.Row="1" Name="PART_SelectedContentHost" ContentSource="SelectedContent"/>
    </Grid>
    <ListBox x:Name="TabItemsListBox" Width="200" Height="200" HorizontalAlignment="Right" VerticalAlignment="Top" Visibility="Collapsed">
        <ListBox.Margin>
            <Thickness Left="0" Top="{Binding to TabItemsList height}" Right="0" Bottom="20"/>
        </ListBox.Margin>
    </ListBox>
</Grid>

我想将ListBox's top Thickness(TabItemsListBox) 绑定到TabItemsList's Height。我怎样才能做到这一点?试过:

{Binding ElementName=TabItemsList, Path=Height}

但是当我运行它时我的程序崩溃了

4

2 回答 2

9

我希望它有效,现在我使用多重绑定。有了这个,您必须提供 4 个绑定,否则它将失败,或者您可以进行测试以防止转换器中出现任何错误。

xml:

 <ListBox x:Name="TabItemsListBox"
             Width="50"
             Height="50">
        <ListBox.Margin>
            <MultiBinding Converter="{StaticResource Converter}">
                <MultiBinding.Bindings>
                    <Binding ElementName="TabItemsListBox"
                             Path="ActualHeight" />
                    <Binding ElementName="TabItemsListBox"
                             Path="ActualHeight" />
                    <Binding ElementName="TabItemsListBox"
                             Path="ActualHeight" />
                    <Binding ElementName="TabItemsListBox"
                             Path="ActualHeight" />
                </MultiBinding.Bindings>
            </MultiBinding>
        </ListBox.Margin>
    </ListBox>

转换器:

public class DoubleToMarginConverter : IMultiValueConverter
{

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var left = (double)values[0];
        var top = (double)values[1];
        var right = (double)values[2];
        var bottom = (double)values[3];

        return new Thickness(left, top, right, bottom);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

最困扰我的是我没有通过多重绑定获得智能感知。我也是新手:)

于 2013-10-26T14:09:13.417 回答
2
    <ListBox x:Name="TabItemsListBox"
             Width="200"
             Height="200"
             HorizontalAlignment="Right"
             VerticalAlignment="Top"
             Visibility="Visible"
             Margin="{Binding ElementName=TabItemsListBox, Path=ActualHeight , Converter={StaticResource Converter}}"
             >
        <ListBoxItem>
            <Button Content="Button" />
        </ListBoxItem>

    </ListBox>

和转换器

 public class DoubleToTopMarginConverter : IValueConverter
 {
     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
     {
         var top = (double)value;

         return new Thickness(0, top, 0, 20);
     }

     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
     {
         throw new NotImplementedException();
     }
  }

这篇文章说它有效,绑定到底部边距,但不适合我。https://stackoverflow.com/a/19454618/1775703

于 2013-10-26T12:57:47.173 回答