2013-10-26 93 views
5

我有下面的XAML代码:WPF绑定属性为另一种元素属性由元素的name

<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)到TabItemsListHeight。 我该怎么做?尝试:

{Binding ElementName=TabItemsList, Path=Height} 

但我的计划粉碎,当我运行它

+0

尝试ActualHeight而不是高度 – iulian3000

+0

仍然粉碎... – Ron

+0

顶部是typeof厚度,这就是为什么它粉碎,并且你正在绑定一个双重厚度。也许在一个转换器的帮助下,你可以将它绑定到顶部 – iulian3000

回答

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

什么是最困扰我的是,我不跟multibinding获得智能。我也是一个新手:)

+0

非常感谢! – Ron

+1

不,谢谢你,因为我没有使用多重绑定。现在我现在是如何工作的:) – iulian3000

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

+0

1.没有转换器的情况下工作。 2.我修改了你的答案,以匹配我的程序,它的工作,但我有一个问题。您返回厚度并将其绑定到边距。我只想绑定Margin.Top,因为我们要绑定Margin.Top的相同方式,我会将Margin.Right绑定到另一个元素上... – Ron

+0

由于我做了错误的事情,我还删除了原始文章中的Edit - 转换为int,我不需要... – Ron

+0

问题是,左,顶..不是依赖属性,这就是为什么我不能直接绑定到它。 – iulian3000