[英]How to change databinding of TextBlock by Click into ItemsControl MVVM?
[英]how to set the generated item for itemscontrol in databinding?
如果我使用ListBox進行數據綁定,則listbox會為每個項目生成一個listboxitem,組合框和comboBoxItem也是如此。 我的問題是-如何為給定的ItemsControl自行設置? (例如,使包含元素為Border)?
用於包裝每個項目的默認項目是ContentPresenter
我不確定您為什么要覆蓋它,因為它沒有視覺外觀或會干擾您的UI的特定行為。
如果要將每個項目都包裝在Border
對象中,則可以設置ItemTemplate
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Blue" BorderThickness="2">
<TextBlock Text="{Binding }" />
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
如果要對ContentPresenter
應用任何特定樣式,請設置ItemContainerStyle
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Grid.Column" Value="{Binding ColumnIndex}" />
<Setter Property="Grid.Row" Value="{Binding RowIndex}" />
</Style>
</ItemsControl.ItemContainerStyle>
用ILSpy挖掘了一些之后-
顯然魔術是在
protected override DependencyObject GetContainerForItemOverride()
{
return new ListBoxItem();
}
這是ListBox所做的工作-在我的控件中,我也應該覆蓋它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.