1

我有一个树视图,想设置height=窗口的高度 -120

我试过这个,但它不起作用。你能帮帮我吗?

谢谢,

张。

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

2 回答 2

2

你需要一个转换器。

创建转换器

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

将转换器添加到资源

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

在绑定中设置转换器

ItemsSource="{Binding NavigationData}" Height="{Binding Path=Height, Converter={StaticResource myConverter}, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" >
于 2012-08-20T19:32:19.497 回答
0

Binding不支持Path您在示例(-120)中的逻辑。根据您要完成的任务,您可以使用VerticalAlignment(将其设置为Stretch) 和Margin.

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

否则,如果您需要数据绑定,您可以使用Converter从传入的值中减去一个值的 a。

于 2012-08-20T19:33:00.203 回答