WPF通过元素的名称将属性绑定到另一个元素属性

时间:2013-10-26 11:50:53

标签: wpf binding

我有以下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的顶部Thickness(TabItemsListBox)绑定到TabItemsList的{​​{1}}。 我怎样才能做到这一点?尝试:

Height

但我的程序在运行时会崩溃

2 个答案:

答案 0 :(得分:8)

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

的Xaml:

 <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();
    }
}

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

答案 1 :(得分: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