簡體   English   中英

在組合框WPF中動態設置所選項目

[英]Set selected item dynamically in combobox wpf

我想在ComboBox中動態選擇1個項目,因為我在DataGrid行中有很多ComboBox。 我嘗試使用SelectedValue或SelectedIndex,但仍然無法正常工作。 請幫我 。 我的代碼在這里

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>

組合框對象

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

DataGrid行對象

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

主要過程

       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;

抱歉,老兄,我嘗試復制您的代碼並弄清楚它能正常工作,但這只是沒有意義,似乎您仍有很多東西要學習。 如果您想學習WPF,則需要知道什么是DataBinding

我的建議是:從小做起,不斷發展。

首先:顯示一個包含字符串的簡單組合框,該組合框由后面的代碼(xaml.cs)填充。 就像是:

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

並在后面的代碼中

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

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

然后使用數據綁定進行操作:

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

只需從代碼后面插入Datacontext即可:

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

並構建視圖模型:

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();
    }
}

(請注意INotifyPropertyChanged的示例實現)

之后,如果需要的話,將其直接構建在數據網格中應該很簡單。

只是使您的步驟變小。

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM