绑定到其他元素(高度减去5px)

时间:2018-03-01 17:39:54

标签: c# wpf vb.net xaml binding

以下代码是okey。

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Window1"
    x:Name="Window1" Title="Window1" 
    Height="200" Width="300">

<Grid>
    <DockPanel LastChildFill="False">
        <Rectangle DockPanel.Dock="Top" Height="{Binding ElementName=Button1, Path=Height}"  Width="70" Fill="Yellow"></Rectangle>
        <Button x:Name="Button1" DockPanel.Dock="Bottom" Background="Red" Height="50" Width="70"/>
    </DockPanel>
</Grid>
</Window>

以下代码需要修复。

<Rectangle DockPanel.Dock="Top" Height="Button1.Height-5px"  Width="70" Fill="Yellow"></Rectangle>

2 个答案:

答案 0 :(得分:0)

我不确定你想要获得什么,但我想要获得高度 - 5,只需使用转换器绑定。

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:younamespace"
    x:Class="Window1"
    x:Name="Window1" Title="Window1" 
    Height="200" Width="300">
<Window.Resources>
<local:RactangleHeightConverters x:Key="NameConverter"/>
</Window.Resources>   
<Grid>
    <DockPanel LastChildFill="False">
         <Rectangle DockPanel.Dock="Top" Height="{Binding ElementName=Button1, Path=Height,Converter={StaticResource NameConverter}}"  Width="70" Fill="Yellow"></Rectangle>
        <Button x:Name="Button1" DockPanel.Dock="Bottom" Background="Red" Height="50" Width="70"/>
    </DockPanel>
</Grid>
</Window>

更多信息 http://www.c-sharpcorner.com/UploadFile/87b416/wpf-value-converters/

使用implements“IValueConverter”

创建类
using System;  

namespace ValueConverters  
{  
    class RactangleHeightConverters:IValueConverter  
    {  
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)  
        {  
            return value - 5;
        }  

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)  
        {  
           return null;
        }  
    }  
}  

答案 1 :(得分:0)

转换器似乎是唯一的方法。但是,在尝试将 ComboBox 宽度与其包含的 GridViewColumn 宽度绑定的特定情况下,在这种情况下,下拉按钮可能会被隐藏。无需使用转换器就可以使用负边框边距来完成此操作。

Drop down button

<ListView x:Name="ListViewObjectMapping" Margin="0,0,0,-5">
    <ListView.View>
        <GridView>
            <GridViewColumn x:Name="GridViewColumnCategoryButtonHidden" Header="Category" Width="200">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox x:Name="ComboBoxButtonHidden" ItemsSource="{Binding Path=Value.LinkedInstances}" SelectedValue="{Binding Path=Value.LinkedInstance}" Width="{Binding Path=Width, Mode=OneWay, ElementName=GridViewColumnCategoryButtonHidden}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>    
            <GridViewColumn x:Name="GridViewColumnCategoryButtonVisible" Header="Category" Width="200">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <Border Margin="-6">
                            <ComboBox x:Name="ComboBoxButtonVisible" ItemsSource="{Binding Path=Value.LinkedInstances}" SelectedValue="{Binding Path=Value.LinkedInstance}" Width="{Binding Path=Width, Mode=OneWay, ElementName=GridViewColumnCategoryButtonVisible}"/>
                        </Border>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>    
        </GridView>
    </ListView.View>
</ListView>