将ListBox绑定到List <string> </string>

时间:2013-05-08 17:05:06

标签: c# .net wpf data-binding listbox

我正在尝试将字符串列表绑定到列表框的内容。出于某种原因,我得到了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
    }
}

2 个答案:

答案 0 :(得分:0)

Dilshod回答(第2条评论):

修复是将我的List更改为ObservableCollection。

一旦我做出改变,一切都按预期工作;谢谢Dilshod!

答案 1 :(得分:0)

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

我正在使用以下简单的解决方案将WPF下拉列表(组合框)以编程方式(C#)绑定到Dictionary对象。在此特定示例中,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。您还可以在我的文章中找到有关下拉控件绑定技术的更多信息:Binding DropDownList to various data structures in Microsoft ASP.NET