5

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

Ron
  • 3,975
  • 17
  • 80
  • 130

2 Answers2

9

I hope it works, now I use multibinding. With this you must provide 4 bindings or it will fail, or you can do your tests to prevent any errors in the converter.

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>

Converter:

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

What is bothering me the most is that I dont get intellisense with multibinding. I'm a newb too :)

iulian3000
  • 1,270
  • 13
  • 26
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>

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

Community
  • 1
  • 1
iulian3000
  • 1,270
  • 13
  • 26
  • 1. Not working without converter. 2. I modified your answer to match my program and it worked but I have one issue. you return Thickness and bind it to the margin. I want to bind only the Margin.Top, because the same way we gonna bind the Margin.Top, I gonna bind the Margin.Right to another element... – Ron Oct 26 '13 at 13:04
  • I also removed my Edit in the original post since I did something wrong - converted double to int which I didnt need to... – Ron Oct 26 '13 at 13:24
  • The problem is that Left, Top.. are not Dependency Properties, and thats why I cant bind directly to it. – iulian3000 Oct 26 '13 at 13:38
  • I see, so is it even possible to achieve what I want? (to bind top to an element's property and right to another element's property) if it is, can u show me how? because I understood what you did to achieve the top binding but have no idea how to chain it with another binding. What I want in the end is that the Top will be binded to the button's ActualHeight and the right will be binded to the button's ActualWidth – Ron Oct 26 '13 at 13:43