【问题标题】:Default item in bound ComboBox绑定 ComboBox 中的默认项
【发布时间】:2012-09-04 11:04:22
【问题描述】:

我有一个绑定ComboBox,我需要在其中显示默认项目“无选择”。 ComboBox 应该在没有文本选择和没有选定项目的情况下关闭,当用户从列表中选择此选项(“无选择”)时。

这些是数据资源

<UserControl.Resources>
    <my:iTimeKeepBaseDataSet x:Key="iTimeKeepBaseDataSet" />
    <CollectionViewSource x:Key="codeSetsViewSource" Source="{Binding Path=codeSets, Source={StaticResource iTimeKeepBaseDataSet}}" />
    <CollectionViewSource x:Key="allMattersViewSource" Source="{Binding Path=allMatters, Source={StaticResource iTimeKeepBaseDataSet}}" />
    <my:CodeIdToDetailsConverter x:Key="codeIdDetailsConverter" />

</UserControl.Resources>

这是 ComboBox 数据模板

                  <DataTemplate x:Key="CodeSetDataCellEditTemplate">
                        <ComboBox DataContext="{StaticResource codeSetsViewSource}"
                                  ItemsSource="{Binding}"
                                  SelectedValuePath="{Binding Path=codeSetId}"
                                  SelectionChanged="OnCodeSetsSelectionChanged"
                                  Style="{StaticResource ComboboxTemplate}">

                            <ComboBox.ItemTemplate>
                                <DataTemplate>

                                    <TextBlock>
                                        <TextBlock.Text>
                                            <MultiBinding StringFormat="{}{0} - {1}">
                                                <Binding Path="codeSetId" />
                                                <Binding Path="codeSetName" />
                                            </MultiBinding>
                                        </TextBlock.Text>
                                    </TextBlock>
                                </DataTemplate>
                            </ComboBox.ItemTemplate>
                        </ComboBox>
                    </DataTemplate>

DataGrid的模板列

<DataGridTemplateColumn x:Name="codeSetId1Column"
                                        Width="SizeToHeader"
                                        CellEditingTemplate="{StaticResource CodeSetDataCellEditTemplate}"
                                        Header="Code Set ID 1"
                                        my:DataGridAttachedProperty.ColumnName="codeSetId1">

请给我一个解决方案。

提前致谢

【问题讨论】:

    标签: wpf styles


    【解决方案1】:

    你可以使用 CompositeCollection

        <CompositeCollection>
           <ComboBoxItem IsEnabled="False" Foreground="Black">Select Item</ComboBoxItem>
           <CollectionContainer Collection="{Binding Source={StaticResource DataKey}}" />    
        </CompositeCollection>
    

    但是,你不能在这里使用 Binding,解决方法是使用 BindingProxy 和 CompositeCollection

    查看my answer这里和cmets了解更多详情

    【讨论】:

    • 感谢重播,我会尽力让你。
    猜你喜欢
    • 1970-01-01
    • 2011-09-07
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-10
    相关资源
    最近更新 更多