将TextBox绑定到字典

时间:2016-05-03 11:44:33

标签: c# wpf dictionary binding bind

我正在尝试在每个标签中创建多个TabItem动态卡片,并且有一个文本框。然后我想将每个文本框绑定到字典索引。例如

  

TAB1 | TAB2 | TAB3

     

TextBox1 | TextBox2 | TextBox3

Dictionary [TAB1]绑定到TextBox1 ... etc

我这样做:

Dictionary<string, string> dic = new Dictionary<string, string>();

            Binding binding = new Binding();
            binding.Source = dic[id];
            binding.Path = ???

            TextBox stb = new TextBox()
            {
                Name = "tb" + id

            };
            stb.SetBinding(TextBox.Text, binding);

我应该在路径中添加什么?

2 个答案:

答案 0 :(得分:2)

您可以使用视图模型动态创建它: 1.创建主视图模型以保留选项卡。

import csv

with open( 'FinexBTCDaily.csv', 'rb' ) as csvfile:
     data = csv.reader( csvfile )

def main( plot ):
    feed = data
  1. 创建将保存字典和标题以显示的TabViewModel。在此示例中,标题是字典的键,但您可以对其进行增强。

    公共类TabViewModel     {         private readonly Dictionary dictionary;

     public class MainViewModel
    {
        private ObservableCollection<TabViewModel> tabs = new ObservableCollection<TabViewModel>();
    
    
        public ObservableCollection<TabViewModel> Tabs
        {
            get { return this.tabs; }
        }
    }
    
  2. 使用itemssource:

    创建tabcontrol
        public TabViewModel(Dictionary<string, string> dictionary)
        {
            this.dictionary = dictionary;
        }
    
        public string Header { get; set; }
        public string TextValue {
            get { return this.dictionary[Header]; }
            set { this.dictionary[Header] = value; }}
    }
    
  3. 为其指定样式

  4. &#13;
    &#13;
        <TabControl VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ItemsSource="{Binding Path=Tabs}" >
            </TabControl>
    
    &#13;
    &#13;
    &#13;

    1. 分配datacontext:

      MainViewModel mainViewModel = new MainViewModel();             词典词典=新词典();             dictionary.Add(&#34; Header1&#34;,&#34; Header 1 text&#34;);             dictionary.Add(&#34; Header2&#34;,&#34; Header 2 text&#34;);

      <Style TargetType="TabItem">
                          <Setter Property="Header" Value="{Binding Path=Header}"/>
                          <Setter Property="ContentTemplate">
                              <Setter.Value>
                                  <DataTemplate DataType="sOfExamples:TabViewModel">
                                  <TextBox Text="{Binding Path=TextValue}"></TextBox>
                                  </DataTemplate>
                              </Setter.Value>
                          </Setter>
                      </Style>
    2. 如果要进行双向绑定更新,可以在tabviewmodel上实现INotifyPropertyChanged。

答案 1 :(得分:1)

这里有一些注意事项:

  1. 为了能够绑定它,字典必须是公共属性:

    Text
  2. 将绑定应用于文本框&#39; stb.SetBinding(TextBox.TextProperty, binding); 属性,您需要引用依赖项属性本身而不是它的值:

    Source
  3. 最后,您要混合绑定的Pathbinding.Path属性:Source是实际绑定目标所属的位置,而DataContext 1}}指的是包含此目标的对象,在这种情况下是当前类(或者,它可以设置文本框&#39; binding.Source = this; binding.Path = new PropertyPath("dic[" + id "]"); 属性,然后自动作为绑定源应用于其所有绑定):

    change
  4. 然而,更优雅的解决方案是绑定整个标签项,如Avneesh的答案所述。