2013-10-26 132 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