我想爲ListBox.ItemTemplate
使用DataTemplate
作爲簡單的待辦事項列表。列表框項目的水平拉伸所需的模板調整
每個ListBoxItem的模板是一個網格,我希望我的第二列的內容能夠展開列表框的剩餘寬度。沒有數額HorizontalAlignment="Stretch"
等等似乎做了詭計,我想我需要修改模板。我看過提取的Xaml模板的ListBox
,但看不到我需要更改的內容。
在這個XAML示例中,您可以看到一個應該展開listboxitem的剩餘寬度的綠框,但沒有。
在XamlPad/WPF中,此代碼實際上按預期呈現。
在Silverlight中,儘管盒子不會伸展。
<ListBox Width="360" Height="150" HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="3,0,3,0" HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="URGENT" Foreground="Red" Margin="5,0,10,0" Grid.Column="0" VerticalAlignment="Center"/>
<Border BorderBrush="Green" BorderThickness="1" Grid.Column="1" HorizontalAlignment="Stretch" Margin="0,2">
<TextBlock Margin="5,2" Text="{Binding}" FontWeight="Bold" />
</Border>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<s:String>Take out trash</s:String>
<s:String>Clean car</s:String>
<s:String>Finish TODO list program</s:String>
<s:String>Sleep</s:String>
</ListBox>
不幸的是一樣的。嘗試了你的想法。我很確定它的模板相關,但我仍然不太瞭解速度,當我用混合提取時知道要編輯什麼 – 2010-07-22 06:36:54
將網格直接放入ListBox作爲內容,看看會發生什麼,我不認爲它是模板相關。每個元素都有什麼寬度? – Ozan 2010-07-22 07:50:08