假设我们有一个绑定到数据库集合的数据源。当然没有空项。如何向ComboBox中添加一个void项,以便在首次加载时用户将看到一个空字符串。我不想向集合中添加虚拟/void对象。最好在XAML中实现。有什么建议吗?
<ComboBox Name="myComboBox" Width="200" Background="White">
<ComboBox.ItemsSource>
<CompositeCollection>
<ComboBoxItem IsEnabled="False" Foreground="Black">Select Item</ComboBoxItem>
<CollectionContainer Collection="{Binding Source={StaticResource DataKey}}" />
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
编辑
正如@surfen在评论中提到的那样,使用BindingProxy是解决绑定问题的一种方法。
<UserControl.Resources>
<CollectionViewSource x:Key="Modules" Source="{Binding Path=Modules}" />
</UserControl.Resources>
<abv:ComboBox SelectedIndex="0" IsNullable="True"
SelectedItem="{Binding Path=SelectedModule, Mode=TwoWay}">
<abv:ComboBox.ItemsSource>
<CompositeCollection>
<ComboBoxItem Content="{DynamicResource EmptyModuleComboBox}"/>
<CollectionContainer Collection="{Binding Source={StaticResource Modules}}" />
</CompositeCollection>
</abv:ComboBox.ItemsSource>
</abv:ComboBox>
public class ComboBox : System.Windows.Controls.ComboBox
{
public static readonly DependencyProperty IsNullableProperty =
DependencyProperty.Register("IsNullable", typeof(bool), typeof(ComboBox));
public bool IsNullable
{
get { return (bool)GetValue(IsNullableProperty); }
set { SetValue(IsNullableProperty, value); }
}
public ComboBox()
{
Loaded += ComboBox_Loaded;
}
void ComboBox_Loaded(object sender, RoutedEventArgs e)
{
if (IsNullable)
{
this.ItemContainerStyle = new Style();
this.ItemContainerStyle.Setters.Add(new EventSetter()
{
Event = ComboBoxItem.PreviewMouseUpEvent,
Handler = new MouseButtonEventHandler(cmbItem_PreviewMouseUp)
});
}
}
public void cmbItem_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
if (Items.IndexOf(sender as ComboBoxItem) == 0)
{
SelectedItem = null;
}
}
}
绑定到MVVM对象:
<ComboBox Name="cbbFiltres" SelectedItem="{Binding ElmtInfo, Mode=TwoWay}" Height="26" MinWidth="90" SelectedIndex="0" SelectedValuePath="Id">
<ComboBox.Resources>
<CollectionViewSource x:Key="cvsFiltres" Source="{Binding Elmts.items}"/>
</ComboBox.Resources>
<ComboBox.ItemsSource>
<CompositeCollection>
<model:tblFiltreChamps Desc="{x:Static resx:resMain.enumAucun}" Id="0"/>
<CollectionContainer Collection="{Binding Source={StaticResource cvsFiltres}}" />
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
<Label Visibility="{Binding Path=SelectedValue, ElementName=cbbFiltres, Converter={StaticResource NullToVisibility}}" />
还有一种通用的转换器:
public class ConvNullToVisibility : IValueConverter {
/// <summary>Convertisseur pour le Get.</summary>
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
if (DesignerProperties.GetIsInDesignMode(new DependencyObject())) return Visibility.Visible; // Pour annuler l'effet dans le designer: https://dev59.com/E5Dea4cB1Zd3GeqPdI-_
return ((value == null) || (string.IsNullOrEmpty(value.ToString())) || (value.ToString() == "0")) ? Visibility.Collapsed : Visibility.Visible;
}
/// <summary>Convertisseur inverse, pour le Set (Binding).</summary>
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
if (value is Visibility) {
return (((Visibility)value) == Visibility.Visible) ? true : false;
} else return false;
}
}
在组合框中声明SelectedValuePath非常重要。
:-)