AustinTX
AustinTX

Reputation: 1352

Manipulate the Height when binding with relative resource in WPF

I have a tree view and want to set the height = the height of the window -120.

I tried this but it does not work. Could you help me out?

thanks,

Jang.

<TreeView x:Name="MyTreeView"  
    ItemsSource="{Binding  NavigationData}" 
    Height="{Binding Path=Height -120, RelativeSource={RelativeSource AncestorType={x:Type  Window}}}" >

Upvotes: 1

Views: 161

Answers (2)

Brian S
Brian S

Reputation: 5785

The Binding does not support logic in the Path as you have above in your example (-120). Depending on what you're trying to accomplish, you may be able to use the VerticalAlignment (set it to Stretch) and the Margin.

<TreeView VerticalAlignment="Stretch" Margin="0,60,0,60"/>

Otherwise, if you need to data bind, you can use a Converter that subtracts a value from the value that is passed in.

Upvotes: 0

steveg89
steveg89

Reputation: 1827

You need a converter.

Create a converter

public class HeightConverter : IValueConverter
{
   public object Convert(object value, Type targetType,
      object parameter, System.Globalization.CultureInfo culture)
     {
        return ((int) value) - 120;
     }
}

Add the converter to resources

<UserControl.Resources>
   <local:HeightConverter x:Key="myConverter" />
</UserControl.Resources>

Set the converter in your binding

ItemsSource="{Binding NavigationData}" Height="{Binding Path=Height, Converter={StaticResource myConverter}, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" >

Upvotes: 2

Related Questions