Reputation: 10162
I'm curious if this can be done... I have a grid that contains several rows. For various design reasons, I would like to maintain the height of the rows as height="Auto"
. Listbox is spanning across three rows within the grid. Unless I set its height to something specific (height="70"
), while items are added or removed, it'll adjust its height dynamically, while also adjusting rows' heights, since they're set to Auto
. This is a normal behavior and everything is functioning as it should -- I'm just trying to get around that behavior.
Is there a way, at all, to have this ListBox stretch vertically within current height of the rows it occupies, while having no direct control over the rows' heights? I understand that row's height property being set to Auto
causes it to expand and contract based on its contents, but I'm wondering if I can prevent my ListBox from having that authority, while allowing other items to do their normal thing.
I can get a fairly precise static height on the ListBox to make the whole interface look good, but I was hoping to have it stretch and occupy the space of the designated row, while maintaing row's height as Auto
and effected by any other content except that ListBox.
If it can't be done, does anyone have a suggestion for what I'm trying to accomplish?
EDIT:
Based on Sheridan's excellent answer, I was able to achieve the desired outcome. Here's the code that made it all happen (also see Sheridan's code).
public class SumConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
double sum = 0;
foreach (var val in values)
{
sum += (double)val;
}
return sum;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException(); // Didn't implement this part yet...
}
}
<Window ....
xmlns:c="clr-namespace:DSTG.Converters
....>
<Window.Resources>
<c:HeightSumConverter x:Key="SumConverter"/>
<Window.Resources>
<ListBox>
<ListBox.Height>
<MultiBinding Converter="{StaticResource SumConverter}">
<Binding Path="ActualHeight" ElementName="txtDat1"/>
<Binding Path="Margin.Bottom" ElementName="txtDat1"/>
<Binding Path="ActualHeight" ElementName="txtDat2"/>
<Binding Path="Margin.Bottom" ElementName="txtDat2"/>
<Binding Path="ActualHeight" ElementName="txtDat3"/>
</MultiBinding>
</ListBox.Height>
</ListBox>
The above code also accounts for the bottom margin of two of the three controls, so that everything aligns
Upvotes: 1
Views: 2093
Reputation: 69979
You should be able to data bind the ListBox.Height
property to the ActualHeight
of another control that takes the same space:
<ListBox Height="{Binding ActualHeight, ElementName=AnotherControlOfTheRightHeight}" />
If you don't have another control spanning 3 rows, you could make a simple IMultiValueConverter
that returns the sum of the input values and data bind to several controls:
<ListBox>
<ListBox.Height>
<MultiBinding Converter="{StaticResource SumConverter}">
<Binding Path="ActualHeight" ElementName="Control1" />
<Binding Path="ActualHeight" ElementName="Control2" />
<Binding Path="ActualHeight" ElementName="Control3" />
</MultiBinding>
</ListBox.Height>
</ListBox>
Upvotes: 2