Reputation:
Following code is 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>
Following code is need to be repaired.
<Rectangle DockPanel.Dock="Top" Height="Button1.Height-5px" Width="70" Fill="Yellow"></Rectangle>
Upvotes: 2
Views: 3181
Reputation: 584
A converter seems to be the only way. But in the specific instance of trying to bind a ComboBox width with its containing GridViewColumn width, in which case the drop down button might get hidden. A negative border margin can be applied to do the trick without using a converter.
<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>
Upvotes: 0
Reputation: 71
I not sure what you wanna obtain, but i you wanna get height - 5, just use binding with converter.
<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>
More info http://www.c-sharpcorner.com/UploadFile/87b416/wpf-value-converters/
Make class with 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;
}
}
}
Upvotes: 4