knockoutJS基于模型值以编程方式进行绑定

时间:2015-05-06 20:23:36

标签: javascript knockout.js

我的html看起来像这样:

 <select data-bind="options: movieInitValues,
                        optionsText: 'Text',
                         optionsValue: 'Value',
                          value: movieSelectedValue,
                           visible: movieSelectVisible"></select>
 <input data-bind="autoMovieComplete: {
     selected: movieSelectedValue,
      selectedName: movieSelectedName},
       visible: showMovieDD,
        disable: promoCodeInvalid" id="MovieName" type="text" />

基本上,我的想法是在某些参数下我会显示一个带有自动完成功能的文本框,在其他情况下我会显示下拉列表。可见&#39;绑定允许我做隐藏,但绑定怎么样。我只希望其中一个绑定到movieSelectedValue,我该如何以编程方式执行此操作?

1 个答案:

答案 0 :(得分:1)

public MainWindow() { // At design time (in your XAML), initialize to the "Loading..." state InitializeComponent() } private async void Window_Loaded(object sender, RoutedEventArgs e) { ClassA hello = new ClassA(); await hello.ConnectAsync(); //List will be used as Source for DataGrid List<object> DataGridViewSource List<object>(); //use the instance of hello to get some Data from the WebService. List<int> objectIds = new List<int>(); objectIds = hello.GetObjectIds("LDAPQuery"); foreach (int id in objectIds) { var tmpObj = await hello.GetObjectByIdAsync(id); DataGridViewSource.Add(tmpObj); } //do binding to DataGrid } binding可行。除非条件的计算结果为true,否则它会阻止任何内部渲染。如果您不想将这些人包装在if中,可以将它与无容器语法结合使用。

它还负责可见性方面,因为它完全从DOM中删除它。

div