1
我正在使用包含两个由分隔符分隔的项目列表的组合框。 我构建这样说:Combobox在分隔符的位置处打开分隔符
public static ObservableCollection<object> Merge<T, U>(IEnumerable<T> collection1, IEnumerable<U> collection2, bool includeSeparator = true)
{
if (collection1 == null || collection2 == null)
{
throw new ArgumentNullException(collection1 == null ? "collection1" : "collection2");
}
List<object> tmp = new List<object>();
tmp.AddRange(collection1.Cast<object>());
if (includeSeparator)
{
tmp.Add(string.Empty);
}
tmp.AddRange(collection2.Cast<object>());
var ret = new ObservableCollection<object>(tmp);
return ret;
}
而在XAML:
<ComboBox
ItemsSource="{Binding Path=AllValues}"
SelectedValue="{Binding Path=SelectedId, Mode=TwoWay, ValidatesOnDataErrors=True}"
SelectedValuePath="Id"
ItemTemplate="{StaticResource CustomItemTemplate}">
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}" BasedOn="{StaticResource {x:Type ComboBoxItem}}">
<Style.Triggers>
<DataTrigger Binding="{Binding}" Value="">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
<Separator HorizontalAlignment="Stretch" IsEnabled="False"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
它工作正常,我有一个分离器,其中存在在列表中插入。 问题是,当SelectedId
为空时,组合框将打开,并在顶部显示分隔符(即滚动条滚动以将分隔符置于列表顶部),如下图所示。
你知道任何方式在顶部列表打开?
在此先感谢。
最近遇到这个问题我自己,而这个解决方案完美地工作。 –