简体   繁体   English

DataBinding Textbox.Text到字符串数组的特定元素

[英]DataBinding Textbox.Text to a specific element of a string array

Delving into the wonderful world of .NET databinding. 深入研究.NET数据绑定的美好世界。 I have a textbox whose text property I want to bind to a specific element of a string array in another object. 我有一个文本框,其文本属性想绑定到另一个对象中字符串数组的特定元素。 (The form contains a combobox that selects the index of the element). (表单包含一个用于选择元素索引的组合框)。

In other words, I'd like to do something like this: 换句话说,我想做这样的事情:

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

where m_Scenario contains the 其中m_Scenario包含

public string[] Fiction { get; set; }

property that I source from. 我从中获取的财产。 Obviously the Binding above won't retrieve my item. 显然,上述绑定不会检索我的物品。 AFAIK I can't create properties that accept parameters. AFAIK我无法创建接受参数的属性。 What's the elegant/correct solution for this when using databinding? 使用数据绑定时,什么是优雅/正确的解决方案? I can think of several ugly-seeming workarounds (ie a string property in m_Scenario that references the array string I'm binding to, and a public function that updates the string property on the combobox SelectedIndexChanged event). 我可以想到几种难看的解决方法(即m_Scenario中的一个字符串属性,它引用了我绑定到的数组字符串,以及一个公共函数,它在组合框SelectedIndexChanged事件上更新了该字符串属性)。

This is an excellent place to have a View Model . 这是拥有View Model的绝佳场所。 Here's another ViewModel link 这是另一个ViewModel链接

What I would do is have the following in the ViewModel (that gets bound to by components on the view) 我要做的是在ViewModel中包含以下内容(受视图上的组件的约束)

an IObservable property bound to the items source of the combo box, that you add/remove to depending on the size of the array 绑定到组合框的item来源的IObservable属性,您可以根据数组的大小添加/删除该属性

a int property for the selected index bound to the SelectedElement of the combo box. 绑定到组合框的SelectedElement的选定索引的int属性。 You would have to do the conversion from string to int when this property is being set. 设置此属性后,您将必须进行从字符串到整数的转换。

a string property that is bound to the textbox.text (you could likely use a label here, by the by) that is updated each time the aforementioned int property for the selected index is changed. 绑定到textbox.text的字符串属性(您可能在此处使用by标记),每次更改所选索引的上述int属性时,该属性就会更新。

If this at all confusing I can build up some pseudo code, but those three properties should work and get you what you want. 如果这完全令人困惑,我可以构建一些伪代码,但是这三个属性应该可以工作,并且可以为您提供所需的东西。

Edit - To add some code: 编辑-添加一些代码:

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

This isn't perfect, but it should give you some idea how you could create a viewmodel with properties that you could bind to. 这不是完美的,但是它应该使您知道如何创建具有可以绑定到的属性的视图模型。 So in your xaml you'd have something like: 因此,在您的xaml中,您会遇到类似以下内容:

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

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

I think you are in WinForms (not WPF), in this case you could just bind directly to the ComboBox's SelectedValue property... 我认为您使用的是WinForms(不是WPF),在这种情况下,您可以直接绑定到ComboBox的SelectedValue属性...

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

Add BindingSource 添加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");
      }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM