Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 组合框项目源=可观察集合&;我需要一个'-无';顶部的虚拟入口_C#_Wpf_Combobox_Observablecollection_Itemsource - Fatal编程技术网

C# 组合框项目源=可观察集合&;我需要一个'-无';顶部的虚拟入口

C# 组合框项目源=可观察集合&;我需要一个'-无';顶部的虚拟入口,c#,wpf,combobox,observablecollection,itemsource,C#,Wpf,Combobox,Observablecollection,Itemsource,我有一个组合框,使用一个可观察的集合作为数据源,我希望框中的第一项是“None”的“dummy”值,因为它控制针对其他数据源的过滤器 其他数据绑定对象也使用相同的可观察集合,因此无法直接向数据源添加“None”值,因为我不希望数据网格中包含“None”。 此外,我宁愿避免过滤器,只删除那些不使用它的“无”值,因为我希望可观察集合直接反映数据库数据;如果可能的话。 我还希望避免每个数据绑定控件都有一个可观察的集合 我真正想要的是将一个非数据绑定的第一个条目放入一个组合框,同时将项目源指向一个obe

我有一个组合框,使用一个可观察的集合作为数据源,我希望框中的第一项是“None”的“dummy”值,因为它控制针对其他数据源的过滤器

其他数据绑定对象也使用相同的可观察集合,因此无法直接向数据源添加“None”值,因为我不希望数据网格中包含“None”。 此外,我宁愿避免过滤器,只删除那些不使用它的“无”值,因为我希望可观察集合直接反映数据库数据;如果可能的话。 我还希望避免每个数据绑定控件都有一个可观察的集合

我真正想要的是将一个非数据绑定的第一个条目放入一个组合框,同时将项目源指向一个obervable集合

谢谢

/编辑:我现在使用的是XAML,但在单击虚拟的第一项时,它不会关闭组合框

<ComboBox x:Class="TestManager.Controls.NullableComboBox"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:w="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"
         xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" MouseUp="ComboBox_MouseUp">
<ComboBox.Resources>
    <Geometry x:Key="DownArrowGeometry">M 0 0 L 3.5 4 L 7 0 Z</Geometry>
    <Style x:Key="ComboBoxReadonlyToggleButton" TargetType="{x:Type ToggleButton}">
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="IsTabStop" Value="false"/>
        <Setter Property="Focusable" Value="false"/>
        <Setter Property="ClickMode" Value="Press"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}">
                        <Grid HorizontalAlignment="Right" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}">
                            <Path x:Name="Arrow" Fill="Black" HorizontalAlignment="Center" Margin="3,1,0,0" VerticalAlignment="Center" Data="{StaticResource DownArrowGeometry}"/>
                        </Grid>
                    </Microsoft_Windows_Themes:ButtonChrome>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="true">
                            <Setter Property="RenderPressed" TargetName="Chrome" Value="true"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Fill" TargetName="Arrow" Value="#AFAFAF"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <ControlTemplate x:Key="ComboBoxMessageTemplate" TargetType="{x:Type ComboBox}">
        <Grid x:Name="MainGrid" SnapsToDevicePixels="true">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
            </Grid.ColumnDefinitions>
            <Popup x:Name="PART_Popup" Margin="1" AllowsTransparency="true" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" Placement="Bottom" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Grid.ColumnSpan="2">
                <Microsoft_Windows_Themes:SystemDropShadowChrome x:Name="Shdw" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=MainGrid}" Color="Transparent">
                    <Border x:Name="DropDownBorder" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1">
                       <ScrollViewer CanContentScroll="true">
                                <StackPanel>
                                    <TextBlock x:Name="SelectMessage" HorizontalAlignment="Left" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag}" VerticalAlignment="Center" Margin="{TemplateBinding Padding}" Visibility="Visible"/>
                                <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.DirectionalNavigation="Contained"/>
                                </StackPanel>
                            </ScrollViewer>
                    </Border>
                </Microsoft_Windows_Themes:SystemDropShadowChrome>
            </Popup>
            <ToggleButton Style="{StaticResource ComboBoxReadonlyToggleButton}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"/>
            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" IsHitTestVisible="false" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Content="{TemplateBinding SelectionBoxItem}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"/>

        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="SelectedItem" Value="{x:Null}">
                <Setter Property="Visibility" TargetName="SelectMessage" Value="Visible"/>
            </Trigger>                
            <Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="true">
                <Setter Property="Margin" TargetName="Shdw" Value="0,0,5,5"/>
                <Setter Property="Color" TargetName="Shdw" Value="#71000000"/>
            </Trigger>
            <Trigger Property="HasItems" Value="false">
                <Setter Property="Height" TargetName="DropDownBorder" Value="95"/>
            </Trigger>
            <Trigger Property="IsEnabled" Value="false">
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                <Setter Property="Background" Value="#FFF4F4F4"/>
            </Trigger>
            <Trigger Property="IsGrouping" Value="true">
                <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</ComboBox.Resources>    

M 0 0 L 3.5 4 L 7 0 Z

这将帮助您

您可以包装ObservableCollection,这将允许您不经修改地使用ComboBox。包装器的外观如下所示:

class PrefixedObservableCollection<T> : Collection<T>, INotifyCollectionChanged, INotifyPropertyChanged
{
    private readonly ObservableCollection<T> _wrappedCollection;
    private readonly T _prefix;

    public IEnumerator<T> GetEnumerator()
    {
        yield return _prefix;
        foreach (T item in _wrappedCollection)
            yield return item;
    }

    // ...  Implement interfaces, etc. Forward along change notifications from wrapped collection.
}
class PrefixedServableCollection:Collection、INotifyCollectionChanged、INotifyPropertyChanged
{
私有只读可观察集合_wrappedCollection;
专用只读T_前缀;
公共IEnumerator GetEnumerator()
{
收益率-收益率前缀;
foreach(包装集合中的T项)
收益回报项目;
}
//…实现接口等。沿包装集合的更改通知转发。
}

谢谢。这让我走了一段路。现在我需要在单击虚拟项时强制关闭下拉列表。。。有什么想法吗?我不得不在那篇链接文章中使用第一个,因为第二个依赖于修改itemsource。所以我现在(经过一些修改)在我的下拉列表中有一个textblock,当选中它时,会将选中的索引设置为-1。。。。但是点击它不会使组合框崩溃。你能发布你工作版本的完整xaml吗?您可以编辑您的帖子。谢谢:以上帖子已编辑到xaml。我更改的部分是将textblock放在“item presenter”上方,以给我一个虚拟的第一个条目。尽管单击它不会关闭列表;我添加了codebehind以将选定索引设置为-1。您可以在触发器中更改selectedIndex。但是当单击textblock时,它没有引发任何属性。幸运的是,这个按钮有一个isPressed属性。如果您想在这种情况下避免代码落后,可以用按钮替换textblock,并相应地更改其外观以使用isPressed属性。