Wpf 如何使用GridViewComboxColumn并允许用户编辑?

Wpf 如何使用GridViewComboxColumn并允许用户编辑?,wpf,xaml,gridview,wpfdatagrid,Wpf,Xaml,Gridview,Wpfdatagrid,我需要呈现一个WPFGridView,其中一列是一个组合框,用户可以从列表中选择一个值或输入一个新值,因此我将IsComboBoxEditable设置为true,但问题是,如果用户键入的值不在ItemsSource中,当组合框松开焦点时,文本为空 注意:当键入新值时,我不希望该值 已添加到项目资源。我只需要将它的字符串值保存在第行中 那是有限度的 我还需要DropDownOpen事件来填充它的ItemsSource 这是我的密码: <telerik:GridViewDataColumn H

我需要呈现一个WPF
GridView
,其中一列是一个组合框,用户可以从列表中选择一个值或输入一个新值,因此我将
IsComboBoxEditable
设置为
true
,但问题是,如果用户键入的值不在
ItemsSource
中,当
组合框松开焦点时,文本为空

注意:当键入新值时,我不希望该值 已添加到
项目资源
。我只需要将它的
字符串
值保存在第行中 那是有限度的

我还需要DropDownOpen事件来填充它的ItemsSource

这是我的密码:

<telerik:GridViewDataColumn Header="Description">
    <telerik:GridViewDataColumn.CellTemplate>
        <DataTemplate>
            <telerik:RadComboBox IsEditable="True" ItemsSource="{Binding Descriptions}" Text="{Binding Description1,Mode=TwoWay}" DropDownOpened="descriptionRadComboBox_DropDownOpened"/>
        </DataTemplate>
    </telerik:GridViewDataColumn.CellTemplate>
</telerik:GridViewDataColumn>


Description1
string
属性,
Descriptions
是运行时填充的
string
列表。(发生
下拉打开事件时)

以下是.net DataGrid控件的解决方案:

  <DataGrid ItemsSource="{Binding Path=Items}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=Title}" ></DataGridTextColumn>
            <DataGridComboBoxColumn SelectedValueBinding="{Binding ComboItem.ID}"  DisplayMemberPath="ComboTitle"  SelectedValuePath="ID">
                <DataGridComboBoxColumn.ElementStyle>
                    <Style TargetType="{x:Type ComboBox}">
                        <Setter Property="ItemsSource" Value="{Binding Path=DataContext.ComboItems, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
                    </Style>
                </DataGridComboBoxColumn.ElementStyle>
                <DataGridComboBoxColumn.EditingElementStyle>                        
                    <Style TargetType="{x:Type ComboBox}">
                        <Setter Property="ItemsSource" Value="{Binding Path=DataContext.ComboItems, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
                        <Setter Property="IsEditable" Value="True" />
                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>                    
            </DataGridComboBoxColumn>
        </DataGrid.Columns>
    </DataGrid>

当然,您也可以使用Telerik DataGrid控件来实现这一点

这是我的ViewModel:

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;  

            ComboItems = new ObservableCollection<ComboItem>()
            {
                new ComboItem(){ID=1,ComboTitle="ComboItem1"},
                new ComboItem(){ID=2,ComboTitle="ComboItem2"},
                new ComboItem(){ID=3,ComboTitle="ComboItem3"}
            };    
            Items = new ObservableCollection<Item>()
            {
                new Item(){ID=1,Title="Item1",ComboItem=ComboItems[0]},
                new Item(){ID=2,Title="Item2",ComboItem=ComboItems[1]},
                new Item(){ID=3,Title="Item3",ComboItem=ComboItems[2]}
            };                
        }    
        public ObservableCollection<Item> Items { get; set; }
        public ObservableCollection<ComboItem> ComboItems { get; set; }               
    }

    public class Item
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public ComboItem ComboItem { get; set; }
    }

    public class ComboItem
    {
        public int ID { get; set; }
        public string ComboTitle { get; set; }
    }
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
DataContext=this;
ComboItems=新的ObservableCollection()
{
新建ComboItem(){ID=1,ComboTitle=“ComboItem1”},
新建ComboItem(){ID=2,ComboTitle=“ComboItem2”},
新建ComboItem(){ID=3,ComboTitle=“ComboItem3”}
};    
Items=新的ObservableCollection()
{
新项(){ID=1,Title=“Item1”,ComboItem=ComboItems[0]},
newitem(){ID=2,Title=“Item2”,ComboItem=ComboItems[1]},
新建项(){ID=3,Title=“Item3”,ComboItem=ComboItems[2]}
};                
}    
公共ObservableCollection项{get;set;}
公共ObservableCollection组合项{get;set;}
}
公共类项目
{
公共int ID{get;set;}
公共字符串标题{get;set;}
公共组合项组合项{get;set;}
}
公共类组合项
{
公共int ID{get;set;}
公共字符串组合标题{get;set;}
}

正如您所提到的,您的目标很简单,就是“可编辑的组合框”。 (当然,您不想向
ItemsSource
添加新项目)


Codebehinde:

private void RadGridView_CellEditEnded(object sender, GridViewCellEditEndedEventArgs e)
{
    if (e.Cell.Column.UniqueName == "description1")
    {
        RadComboBox combo = e.Cell.ChildrenOfType<RadComboBox>().FirstOrDefault();
        if (combo != null)
        {
            List<Description> comboItems = combo.ItemsSource as List<Description>;

            string textEntered = e.Cell.ChildrenOfType<RadComboBox>().First().Text;

            bool result = comboItems.Contains(comboItems.Where(x => x.DescriptionTitle == textEntered).FirstOrDefault());
            if (!result)
            {
                comboItems.Add(new Description { DescriptionTitle = textEntered });
                combo.SelectedItem = new Description { DescriptionTitle = textEntered };
            }
            if (_viewModel.AccDocumentItem != null)
            {
                if (e.Cell.Column.UniqueName == "description1")
                    _viewModel.AccDocumentItem.Description1 = textEntered;
            }
        }
    }
}
private void RadGridView\u CellEditEnded(对象发送方,GridViewCellEditEndedEventArgs e)
{
if(e.Cell.Column.UniqueName==“description1”)
{
RadComboBox combo=e.Cell.ChildrenOfType().FirstOrDefault();
如果(组合!=null)
{
List comboItems=combo.ItemsSource作为列表;
字符串textEntered=e.Cell.ChildrenOfType().First().Text;
bool result=comboItems.Contains(comboItems.Where(x=>x.DescriptionTitle==textEntered.FirstOrDefault());
如果(!结果)
{
添加(新描述{DescriptionTitle=textEntered});
combo.SelectedItem=新描述{DescriptionTitle=textEntered};
}
如果(_viewModel.AccDocumentItem!=null)
{
if(e.Cell.Column.UniqueName==“description1”)
_viewModel.AccDocumentItem.Description1=textEntered;
}
}
}
}

显示相关的V和VM代码片段我需要WPF DataGrid中的可编辑ComboBoxColumn。感谢您的评论,我已经添加了我的代码片段。
private void RadGridView_CellEditEnded(object sender, GridViewCellEditEndedEventArgs e)
{
    if (e.Cell.Column.UniqueName == "description1")
    {
        RadComboBox combo = e.Cell.ChildrenOfType<RadComboBox>().FirstOrDefault();
        if (combo != null)
        {
            List<Description> comboItems = combo.ItemsSource as List<Description>;

            string textEntered = e.Cell.ChildrenOfType<RadComboBox>().First().Text;

            bool result = comboItems.Contains(comboItems.Where(x => x.DescriptionTitle == textEntered).FirstOrDefault());
            if (!result)
            {
                comboItems.Add(new Description { DescriptionTitle = textEntered });
                combo.SelectedItem = new Description { DescriptionTitle = textEntered };
            }
            if (_viewModel.AccDocumentItem != null)
            {
                if (e.Cell.Column.UniqueName == "description1")
                    _viewModel.AccDocumentItem.Description1 = textEntered;
            }
        }
    }
}