我做了一個ComboBox繼承自ComboBox.
我想默認為我的自定義啟用虛擬化ComboBox。
我正在考慮在ComboBox建構式中這樣做,但我不知道這是否可能。這將使我避免每次都必須添加以下代碼。
<CustomComboBox>
<CustomComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</CustomComboBox.ItemsPanel>
</CustomComboBox>
你知道我該怎么做嗎?
uj5u.com熱心網友回復:
您可以創建 aStyle并將其添加到ResourceDictionary所需范圍內的 a 中:
應用程式.xaml
<Style TargetType="ComboBox">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
或者,擴展ComboBox和硬編碼Panel:
public class VirtualizingComboBox : ComboBox
{
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
string itemsPanelTemplateText = @"
<ItemsPanelTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>
<VirtualizingStackPanel />
</ItemsPanelTemplate>";
ItemsPanelTemplate temsPanelTemplate = System.Windows.Markup.XamlReader.Parse(itemsPanelTemplateText) as ItemsPanelTemplate;
this.ItemsPanel = temsPanelTemplate ;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/481277.html
