Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Bind property to another element property by the element's name

Tags:

binding

wpf

I have the following 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>

I want to bind the ListBox's top Thickness (TabItemsListBox) to the TabItemsList's Height. How can I do that? tried:

{Binding ElementName=TabItemsList, Path=Height}

but my program crushes when I run it

like image 803
Ron Avatar asked Oct 26 '13 11:10

Ron


1 Answers

    <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>

and the converter

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

This post sais that it works, binding to Bottom margin, but not for me. https://stackoverflow.com/a/19454618/1775703

like image 138
iulian3000 Avatar answered Oct 27 '22 17:10

iulian3000