Hi buddies,
I am newbie to wpf programmer,now I add a listbox to a window and edit the listbox container style to include a textblock and rectangle in a gridpanel, I've bound the item data to a datasource and it works, now the question is I want to the listbox item height can dynamically changd to fill all the listbox, if the listbox height is 100, if have 5 items then each item height is 20, but if just 2 then the item height is 50.
My thought is to calculate the item height when window load, but how can i set to value to the listbox?
below is the xaml segment
<Style x:Key="LeftListBoxItemContainer" TargetType="{x:Type ListBoxItem}"><Setter Property="Background" Value="Transparent"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type ListBoxItem}"><Grid x:Name="grid" Width="Auto" Height="Auto"><Rectangle x:Name="rectangle" Stroke="#7F000000" Margin="0" VerticalAlignment="Top" Height="50"><Rectangle.Fill><LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"><GradientStop Color="#7FFFFFFF" Offset="0"/><GradientStop Color="#7F0ED8E2" Offset="1"/></LinearGradientBrush></Rectangle.Fill></Rectangle><TextBlock x:Name="Content" HorizontalAlignment="Stretch" VerticalAlignment="Center" Foreground="#FF20C00C" Text="{Binding Name, Mode=Default}" TextAlignment="Center" TextWrapping="Wrap"/></Grid></ControlTemplate></Setter.Value></Setter></Style>
I ever created a property in MainWindow called ItemHeight and bind it to grid's height, but does not work.
And i tried mylistbox.Template.FindName("gird", mylistbox) but it return null
Could someone please help me? thanks!