簡體   English   中英

DataBinding Textbox.Text到字符串數組的特定元素

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

深入研究.NET數據綁定的美好世界。 我有一個文本框,其文本屬性想綁定到另一個對象中字符串數組的特定元素。 (表單包含一個用於選擇元素索引的組合框)。

換句話說,我想做這樣的事情:

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

其中m_Scenario包含

public string[] Fiction { get; set; }

我從中獲取的財產。 顯然,上述綁定不會檢索我的物品。 AFAIK我無法創建接受參數的屬性。 使用數據綁定時,什么是優雅/正確的解決方案? 我可以想到幾種難看的解決方法(即m_Scenario中的一個字符串屬性,它引用了我綁定到的數組字符串,以及一個公共函數,它在組合框SelectedIndexChanged事件上更新了該字符串屬性)。

這是擁有View Model的絕佳場所。 這是另一個ViewModel鏈接

我要做的是在ViewModel中包含以下內容(受視圖上的組件的約束)

綁定到組合框的item來源的IObservable屬性,您可以根據數組的大小添加/刪除該屬性

綁定到組合框的SelectedElement的選定索引的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}"/>

我認為您使用的是WinForms(不是WPF),在這種情況下,您可以直接綁定到ComboBox的SelectedValue屬性...

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

添加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