简体   繁体   English

在组合框WPF中动态设置所选项目

[英]Set selected item dynamically in combobox wpf

I want to select 1 item in ComboBox dynamically because i have many ComboBox in DataGrid row . 我想在ComboBox中动态选择1个项目,因为我在DataGrid行中有很多ComboBox。 I try using SelectedValue or SelectedIndex but it still not working . 我尝试使用SelectedValue或SelectedIndex,但仍然无法正常工作。 Please help me . 请帮我 。 My code here 我的代码在这里

Xaml file : XAML文件:

<DataGridTemplateColumn  Header="DataType_Id">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox Loaded="cbxDataType_Loaded" Name="cbxDataType" SelectionChanged="ComboBox_SelectionChanged" 
                     SelectedValuePath="Id"
                      DisplayMemberPath="Name"
                      ItemsSource="{Binding Path=masterData}"
                      SelectedValue="{Binding Path=ComboboxObj,Mode=TwoWay}"
                      SelectedItem="{Binding Path=seletedItem}"
                      DataContext="{Binding}">
            </ComboBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

ComboBox Object 组合框对象

public class ComboboxObj
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    //public string selectedItem { get; set; }
}

DataGrid row Object DataGrid行对象

public class ListDataExtract
{
    public SP_List_Details Detail { get; set; }
    public List<ComboboxObj> masterData { get; set; }
    public ComboboxObj seletedItem { get; set; }
}

Main Process 主要过程

       for (int i = 0; i < lstDetail.Count; i++)
            {
                ComboboxObj cbxObj = new ComboboxObj();
                ListDataExtract extract = new ListDataExtract();
                extract.Detail = lstDetail[i];
                extract.masterData = lstCbx;
                // Create Seleted Item
                cbxObj.Id = lstDetail[i].DataType_Id.Value;
                cbxObj.Name = findIndexMasterData(lstDetail[i].DataType_Id.Value, lstCbx);
                // lstCbx is List Object ComboboxObj
                extract.seletedItem = lstCbx[0];
                // End Create Seleted Item
                lstExtract.Add(extract);
            }
            DataGridListDetails.ItemsSource = lstExtract;

Sorry dude, I try to copy your code and figure out to make it work, but it just does not make sense, seems like you still have a lot to learn. 抱歉,老兄,我尝试复制您的代码并弄清楚它能正常工作,但这只是没有意义,似乎您仍有很多东西要学习。 You need to know what is DataBinding if you want to learn WPF. 如果您想学习WPF,则需要知道什么是DataBinding

My advice would be: start little and make it grow. 我的建议是:从小做起,不断发展。

First of all: display a simple combobox containing string, populated from code behind (xaml.cs). 首先:显示一个包含字符串的简单组合框,该组合框由后面的代码(xaml.cs)填充。 Something like: 就像是:

<Grid>
   <ComboBox x:Name="MyCombo"></ComboBox>
</Grid>

And in the code behind 并在后面的代码中

    var myComboItemsSource = new List<string>();
    MyCombo.ItemsSource = myComboItemsSource;

    myComboItemsSource.Add("hello");
    myComboItemsSource.Add("world");

Then do it with databinding: 然后使用数据绑定进行操作:

<ComboBox ItemsSource="{Binding MyComboItemsSource}" SelectedItem="{Binding SelectedItem}"></ComboBox>

Simply inject the Datacontext from codebehind: 只需从代码后面插入Datacontext即可:

public MainWindow()
{
    InitializeComponent();
    DataContext = new ViewModel();
}

And build the view model: 并构建视图模型:

public abstract class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var propertyChanged = PropertyChanged;

        propertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
public class ViewModel : ViewModelBase
{
    private string _selectedItem;
    public List<string> MyComboItemsSource { get; }

    public string SelectedItem
    {
        get { return _selectedItem; }
        set
        {
            _selectedItem = value;
            NotifyPropertyChanged();
        }
    }

    public ViewModel()
    {
        MyComboItemsSource = new List<string>();
        MyComboItemsSource.Add("hello");
        MyComboItemsSource.Add("world");

        SelectedItem = MyComboItemsSource.First();
    }
}

(Note the example implementation of INotifyPropertyChanged) (请注意INotifyPropertyChanged的示例实现)

After that, it should be straightforward to build it in a datagrid if you want to. 之后,如果需要的话,将其直接构建在数据网格中应该很简单。

Just make your step smaller. 只是使您的步骤变小。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM