我找到了覆盖模板选择的代码: DataTemplateSelector
但是组合框的崩溃状态不起作用。
这是XAML:
<telerik:RadComboBox x:Name="cboPickups"
Command="{Binding PickupChangedCommand}" HorizontalAlignment="Stretch"
CommandParameter="{Binding ElementName=cboPickups,Path=SelectedItem}" IsEnabled="{Binding CanSelectPickup}"
ItemsSource="{Binding Pickups}"
SelectedValuePath="CustomerId" SelectedValue="{Binding Path=SelectedPickup.Id, Mode=OneWay}" Margin="4">
<telerik:RadComboBox.ItemTemplateSelector>
<Helpers:CustomerComboTemplateSelector />
</telerik:RadComboBox.ItemTemplateSelector>
</telerik:RadComboBox>
以下是模板:
<DataTemplate x:Key="CustomerComboCollapsed">
<TextBlock Text="{Binding CompanyName}" />
</DataTemplate>
<DataTemplate x:Key="CustomerComboExpanded">
<StackPanel>
<TextBlock Text="{Binding CompanyName}" />
<TextBlock Text="{Binding CityState}" />
</StackPanel>
</DataTemplate>
这是模板选择器类:
public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
{
var presenter = (ContentPresenter) container;
if (presenter != null && item != null)
{
if (presenter.TemplatedParent is ComboBox)
{
return presenter.FindResource("CustomerComboCollapsed") as DataTemplate;
}
else
{
return presenter.FindResource("CustomerComboExpanded") as DataTemplate;
}
}
return null;
}
但是当窗口初始化并尝试加载ComboBox
时,&#34;容器&#34;一片空白。
所以它尝试分配折叠的模板,但由于某种原因,容器为空。 有什么想法吗?
谢谢, -sid。