Wpf 有空项的组合框?
假设我们有一个数据源绑定到数据库中的一个集合。当然没有空项。如何将无效项添加到组合框中,以便用户在首次加载时看到空字符串。我不想将虚拟/无效对象添加到集合中以XAML为最佳模式。有什么建议吗?Wpf 有空项的组合框?,wpf,combobox,Wpf,Combobox,假设我们有一个数据源绑定到数据库中的一个集合。当然没有空项。如何将无效项添加到组合框中,以便用户在首次加载时看到空字符串。我不想将虚拟/无效对象添加到集合中以XAML为最佳模式。有什么建议吗? <ComboBox Name="myComboBox" Width="200" Background="White"> <ComboBox.ItemsSource> <CompositeCollection>
<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>
选择项
编辑
如评论中所述,是解决绑定问题的变通方法
<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;
}
}
}
公共类组合框:System.Windows.Controls.ComboBox
{
公共静态只读DependencyProperty IsNullableProperty=
DependencyProperty.Register(“IsNullable”、typeof(bool)、typeof(ComboBox));
公共布尔值为空
{
获取{return(bool)GetValue(IsNullableProperty);}
set{SetValue(IsNullableProperty,value);}
}
公共组合框()
{
已加载+=组合框\已加载;
}
已加载无效组合框(对象发送器,路由目标e)
{
如果(可为空)
{
this.ItemContainerStyle=新样式();
this.ItemContainerStyle.Setters.Add(neweventsetter())
{
Event=ComboBoxItem.PreviewMouseUpEvent,
Handler=新鼠标按钮Venthandler(cmbItem_PreviewMouseUp)
});
}
}
公共无效cmbItem_PreviewMouseUp(对象发送器,鼠标按钮Ventargs e)
{
if(Items.IndexOf(发件人作为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: http://stackoverflow.com/questions/33401900/wpf-detect-design-mode-in-a-converter
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;
}
}
公共类ConvNullToVisibility:IValueConverter{
///兑换商。
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性){
if(DesignerProperties.GetIsInDesignMode(new DependencyObject())返回Visibility.Visible;//倾倒环空l'effet dans le designer:http://stackoverflow.com/questions/33401900/wpf-detect-design-mode-in-a-converter
return((value==null)| |(string.IsNullOrEmpty(value.ToString())| |(value.ToString()==0”)?可见性。折叠:可见性。可见;
}
///Convertisseur倒数,倒数集(绑定)。
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性){
if(值为可见性){
返回(((可见性)值)=可见性.可见)?真:假;
}否则返回false;
}
}
在组合框中声明SelectedValuePath非常重要。
:-)试试Mahapps组合框 xmlns:controls=”http://metro.mahapps.com/winfx/xaml/controls"
请参阅编辑的文章,您只需将IsEnabled=“False”front=“Black”添加到items属性我似乎无法在ViewModel绑定场景中实现这一点。。。有什么想法吗?啊。。。我想出来了。CompositeCollection不可冻结,因此无法与绑定一起使用。不幸的是,我找到了一种解决CompositeCollection问题的方法:@surfen因为你的博客已经移动了这是新的url:注意,提供的解决方案不适用于绑定。我找到了一种解决绑定问题的方法。请看这篇文章:什么是EmptyModuleComboBox?
<ComboBox x:Name="bars" **controls:TextBoxHelper.ClearTextButton="True"**
DisplayMemberPath="Name"
Height="21"
SelectedItem="{Binding Bar}"/>