-1

我正在尝试将字符串列表绑定到列表框的内容。出于某种原因,我得到了 bluetape 列表的结果,但 BluetapeList 的内容从未进入列表框。任何帮助将非常感激!

XAML:

    <ListBox
            Name="lbxTapeIn"
            Grid.Row="1"
            Grid.Column="1"
            Grid.ColumnSpan="1"
            Width="70"
            Height="80"
            SelectionChanged="TapeSelectionChanged"
            ItemsSource="{Binding}"
            SelectedValue="{Binding SelectedBt}"
            Background="DeepSkyBlue"
            Foreground="MidnightBlue"                
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Margin="5"/>

代码背后:

public partial class OverrideAoiBinningWindow : Window
{
    private OverrideAoiBinningWindowViewModel ovAoiBinWin;
    public OverrideAoiBinningWindow()
    {
        InitializeComponent();
        ovAoiBinWin = new OverrideAoiBinningWindowViewModel(tvwWaferList, txtFilter);
        AssignDataContexts();
    }
    private void AssignDataContexts()
    {
        btnChooseWafer.DataContext = ovAoiBinWin;
        btnSave.DataContext = ovAoiBinWin;
        txtWafer.DataContext = ovAoiBinWin;
        cbxAoiState.DataContext = ovAoiBinWin;
        lbxTapeIn.DataContext = ovAoiBinWin.BluetapeList;
    }
    private void TapeSelectionChanged(object sender, RoutedEventArgs e)
    {
        if (!string.IsNullOrEmpty(ovAoiBinWin.SelectedWafer))
        {
            if (cbxAoiState.SelectedValue != null)
            {
                btnSave.IsEnabled = true;
            }
        }
    }
    private void AoiStateChanged(object sender, RoutedEventArgs e)
    {
        if (!string.IsNullOrEmpty(ovAoiBinWin.SelectedWafer))
        {
            if (lbxTapeIn.SelectedValue != null)
            {
                btnSave.IsEnabled = true;
            }
        }
    }
    private void Close(object sender, RoutedEventArgs e)
    {
        this.Close();
    }
}

查看型号:

public class OverrideAoiBinningWindowViewModel : ViewModelBase, ISelectWafers
{
    public OverrideAoiBinningWindowViewModel(TreeView tvwWaferList, TextBox txtFilter)
    {
        // Set private fields
        this.tvwWaferList = tvwWaferList;
        this.txtFilter = txtFilter;

        // Instantiate objects and initialize settings
        this.InstantiateObjects();
        this.SetControlSettings();

        // Run the initialization thread
        initThread.RunWorkerAsync();
    }
    public string SelectedWafer
    {
        get
        {
            return selectedWafer;
        }
        set
        {
            selectedWafer = value;
            OnPropertyChanged("SelectedWafer");
        }
    }
    public string SelectedBt
    {
        get
        {
            return selectedBt;
        }
        set
        {
            selectedBt = value;
            OnPropertyChanged("SelectedBt");
        }
    }
    public string SelectedAoiState
    {
        get
        {
            return selectedAoiState;
        }
        set
        {
            selectedAoiState = value;
            OnPropertyChanged("SelectedAoiState");
        }
    }
    public List<string> AOIStateList
    {
        get
        {
            return aoiStateList;
        }
        set
        {
            aoiStateList = value;
            OnPropertyChanged("AOIStateList");
        }
    }
    public List<string> BluetapeList
    {
        get
        {
            return bluetapeList;
        }
        set
        {
            bluetapeList = value;
            OnPropertyChanged("BluetapeList");
        }
    }
    public ICommand SelectWaferCommand
    {
        get
        {
            if (selectWaferCommand == null)
            {
                selectWaferCommand = new DelegateCommand(SelectWafer);
            }
            return selectWaferCommand;
        }
    }
    public ICommand SaveAoiStateCommand
    {
        get
        {
            if (saveAoiStateCommand == null)
            {
                saveAoiStateCommand = new DelegateCommand(SaveAoiState);
            }
            return saveAoiStateCommand;
        }
    }

    private void InstantiateObjects()
    {
        initThread = new BackgroundWorker();
        aoiStateList = new List<string>();
        bluetapeList = new List<string>();
        converter = new WaferIDConverter();
    }
    private void SetControlSettings()
    {
        initThread.WorkerReportsProgress = false;
        initThread.WorkerSupportsCancellation = false;
        initThread.DoWork += InitThread_DoWork;
        initThread.RunWorkerCompleted += InitThread_RunWorkerCompleted;
    }
    private void PopulateAoiStateList()
    {
        aoiStateList.Add("True");
        aoiStateList.Add("False");
        aoiStateList.Add("NotBinned");
        aoiStateList.Add("NeverAOI");
    }
    private void PopulateBluetapeList()
    {
        waferQueries = new WaferQueries(
            DataLibrary.GetSingulationOne(selectedWafer));
        foreach (BlueTape tape in waferQueries.GetBlueTapeList())
        {
            bluetapeList.Add(tape.Name);
        }
        OnPropertyChanged("BluetapeList");
    }
    private void SaveAoiState()
    {
        Mouse.OverrideCursor = Cursors.Wait;
        singOne = new SingOneTable();
        singOne.OverrideAoiState(selectedWafer, selectedBt, selectedAoiState);
        Mouse.OverrideCursor = null;
        MessageBox.Show(
            "The AOI state of " + selectedBt + " from " + selectedWafer +
            " has been successfully changed to " + selectedAoiState + "!",
            "AOI State Saved", MessageBoxButton.OK, MessageBoxImage.Exclamation);
    }
    public void SelectWafer()
    {
        Mouse.OverrideCursor = Cursors.Wait;
        SelectedWafer = tvwWaferList.SelectedValue.ToString();
        PopulateBluetapeList();
        Mouse.OverrideCursor = null;
    }

    private void InitThread_DoWork(object sender, DoWorkEventArgs e)
    {
        if (!handled)
        {
            PopulateAoiStateList();
            tvwPresenter = new TreeViewPresenter(tvwWaferList, txtFilter, this);
            tvwPresenter.WaferList = DataLibrary.GetWaferList();
            handled = true;
        }
    }
    private void InitThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        tvwPresenter.TreeView.DataContext = tvwPresenter.ProcessesAndWafers;
        tvwPresenter.WaferListCache = tvwPresenter.ProcessesAndWafers;
        tvwPresenter.ProcessArray = tvwPresenter.WaferListCache.ToArray();
        //TODO: Update Status text block
    }
}
4

2 回答 2

0

Dilshod 回答(第二条评论):

解决方法是将我的列表更改为 ObservableCollection。

一旦我做出改变,一切都按预期工作。谢谢迪尔肖德!

于 2013-05-08T17:24:46.293 回答
0

动态绑定 WPF 下拉控件 (C#)。

Dictionary我正在使用以下简单的解决方案以编程方式(C#)将 WPF 下拉列表(组合框)绑定到对象。在此特定示例Dictionary中,包含具有相应 2 位国家代码(键)的国家列表:

清单 1. Dictionary 对象包含带 2 位国家代码的国家列表

Dictionary<string, string> _co = new Dictionary<string, string>();
_co.Add(String.Empty, String.Empty);
_co.Add("US", "United States");
_co.Add("CA", "Canada");
_co.Add("MX", "Mexico");

清单 2. 将下拉列表绑定到 Dictionary 对象 (WPF/C#)

// binding to country list Dictionary object (_co)
_cmbCountry.ItemsSource = _co;

// Country 2-digit Code used as a key
_cmbCountry.SelectedValuePath = _dKey;

// Country Name (string to display)
_cmbCountry.DisplayMemberPath = _dValue;

 // first index selected
_cmbCountry.SelectedIndex = 0;

 // DropDownClosed event subscription using Lambda notation
_cmbCountry.DropDownClosed += (s, e) => ComboBox_Closed(s, e);

上面的代码片段(清单 2.)还显示了如何使用 'short-cut' Lambda 样式订阅控制事件。希望这会有所帮助。问候, AB

PS。您还可以在我的文章中找到有关下拉控件绑定技术的更多信息:将 DropDownList 绑定到 Microsoft ASP.NET 中的各种数据结构

于 2013-05-08T17:25:44.860 回答