【问题标题】:DataBinding Textbox.Text to a specific element of a string arrayDataBinding Textbox.Text 到字符串数组的特定元素
【发布时间】:2011-11-12 10:13:08
【问题描述】:

深入了解 .NET 数据绑定的精彩世界。我有一个文本框,我想将其文本属性绑定到另一个对象中字符串数组的特定元素。 (表单包含一个选择元素索引的组合框)。

换句话说,我想做这样的事情:

textBoxFictionShort.DataBindings.Add(
                new Binding("Text", m_Scenario, "Fiction[int32.Parse(comboBoxSelector.Text)]"));

其中 m_Scenario 包含

public string[] Fiction { get; set; }

我来源的财产。显然,上面的 Binding 不会检索我的项目。 AFAIK 我无法创建接受参数的属性。使用数据绑定时,优雅/正确的解决方案是什么?我可以想到几个看起来难看的解决方法(即 m_Scenario 中的一个字符串属性引用了我要绑定到的数组字符串,以及一个更新组合框 SelectedIndexChanged 事件上的字符串属性的公共函数)。

【问题讨论】:

  • 很抱歉仍未将此问题标记为已回答。我陷入了完全不同的事情,但会回到这个问题并根据我最终使用的内容进行标记。

标签: c# winforms arrays data-binding textbox


【解决方案1】:

这是拥有View Model 的好地方。 Here's another ViewModel link

我要做的是在 ViewModel 中有以下内容(由视图上的组件绑定)

绑定到组合框项目源的 IObservable 属性,您可以根据数组的大小添加/删除该属性

绑定到组合框的 SelectedElement 的选定索引的 int 属性。设置此属性时,您必须执行从字符串到 int 的转换。

绑定到 textbox.text 的字符串属性(您可能在此处使用标签 by),每次更改所选索引的上述 int 属性时都会更新。

如果这完全令人困惑,我可以构建一些伪代码,但这三个属性应该可以工作并得到你想要的。

编辑 - 添加一些代码:

public class YourViewModel : DependencyObject {
    public string[] FictionArray {get; private set;}

    public IObservable<string> AvailableIndices;

    public static readonly DependencyProperty SelectedIndexProperty=
      DependencyProperty.Register("SelectedIndex", typeof(string), typeof(YourViewModel), new PropertyMetadata((s,e) => {
        var viewModel = (YourViewModel) s;
        var index = Convert.ToInt32(e.NewValue);
        if (index >= 0 && index < viewModel.FictionArray.Length)
            viewModel.TextBoxText=FictionArray[index];
      }));

    public bool SelectedIndex {
      get { return (bool)GetValue(SelectedIndexProperty); }
      set { SetValue(SelectedIndexProperty, value); }
    }

    public static readonly DependencyProperty TextBoxTextProperty=
      DependencyProperty.Register("TextBoxText", typeof(string), typeof(YourViewModel));

    public bool TextBoxText {
      get { return (bool)GetValue(TextBoxTextProperty); }
      set { SetValue(TextBoxTextProperty, value); }
    }

    public YourViewModel(string[] fictionArray) {
        FictionArray = fictionArray;
        for (int i = 0; i < FictionArray.Length; i++){
            AvailableIndices.Add(i.ToString()));
        }
    }
}

这并不完美,但它应该让您了解如何创建具有可以绑定的属性的视图模型。所以在你的 xaml 中你会有类似的东西:

<ComboBox ItemSource="{Binding AvailableIndices}" SelectedItem="{Binding SelectedIndex}"/>

<TextBox Text="{Binding TextBoxText}"/>

【讨论】:

  • 从重读中我意识到我可能误解了你所说的。我以为您想要一个组合框,其中包含 (o, 1, ..., n-1) 之类的条目,用于称为 FicionArray 的数组的索引。如果您希望组合框在数组中包含实际的字符串,您可以通过将 TextBox 绑定到 SelectedItemProperty 并进行更改,使其不执行任何花哨的转换或 TextBoxText 设置。 (实际上,完全删除 TextBoxText 和 TextBoxTextProperty。)
【解决方案2】:

我认为您使用的是 WinForms(不是 WPF),在这种情况下,您可以直接绑定到 ComboBox 的 SelectedValue 属性...

comboBox1.DataSource = m_Scenario.Fiction;
textBoxFictionShort.DataBindings.Add(new Binding("Text", comboBox1, "SelectedValue"));

【讨论】:

    【解决方案3】:

    添加BindingSource

            ...
            bindingSource1.DataSource = m_Scenario.Fiction
                .Select((x, i) => new {Key = i + 1, Value = x})
                .ToDictionary(x => x.Key, x => x.Value);
            comboBox1.DisplayMember = "Key";
            comboBox1.DataSource = bindingSource1;
            textBox1.DataBindings.Add("Text", bindingSource1, "Value");
          }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-18
      • 2015-03-08
      • 1970-01-01
      • 1970-01-01
      • 2021-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多