【问题标题】:Bind TextBox to Dictionary将文本框绑定到字典
【发布时间】:2016-08-28 10:41:00
【问题描述】:

我正在尝试动态创建多个 TabItem,每个选项卡都有一个文本框。然后我想将每个文本框绑定到一个字典索引。例如

TAB1 |选项卡2 | TAB3

文本框1 |文本框2 |文本框3

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

我正在这样做:

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

我应该在路径中放什么?

【问题讨论】:

    标签: c# wpf dictionary binding bind


    【解决方案1】:

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

     public class MainViewModel
    {
        private ObservableCollection<TabViewModel> tabs = new ObservableCollection<TabViewModel>();
    
    
        public ObservableCollection<TabViewModel> Tabs
        {
            get { return this.tabs; }
        }
    }
    
    1. 创建将保存字典和要显示的标题的 TabViewModel。在此示例中,标题是字典的键,但您可以对其进行增强。

      公共类 TabViewModel { 私有只读字典字典;

          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; }}
      }
      
    2. 使用 itemssource 创建选项卡控件:

          <TabControl VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ItemsSource="{Binding Path=Tabs}" >
              </TabControl>
      
    3. 为其指定样式

    <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>
    1. 分配数据上下文:

      MainViewModel mainViewModel = new MainViewModel(); 字典字典 = new Dictionary(); dictionary.Add("Header1", "Header 1 text"); dictionary.Add("Header2", "Header 2 text");

              mainViewModel.Tabs.Add(new TabViewModel(dictionary)
              {
                  Header = "Header1"
              });
              mainViewModel.Tabs.Add(new TabViewModel(dictionary)
              {
                  Header = "Header2"
              });
      
              this.DataContext = mainViewModel;
      

    如果你想要双向绑定更新,你可以在 tabviewmodel 上实现 INotifyPropertyChanged。

    【讨论】:

    • 非常感谢您的解释。我会尝试并提供反馈
    【解决方案2】:

    这里有几点需要注意:

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

      public Dictionary<string, string> dic { get; set; } = new Dictionary<string, string>();
      
    2. 要将绑定应用到文本框的Text 属性,您需要引用依赖属性本身而不是其值:

      stb.SetBinding(TextBox.TextProperty, binding);
      
    3. 最后,您将绑定的 SourcePath 属性混合在一起:binding.Path 是实际绑定目标所在的位置,而 Source 指的是持有此目标的对象,在本例中是当前类(或者,可以设置文本框的 DataContext 属性,然后自动将其作为绑定源应用于其所有绑定):

      binding.Source = this;
      binding.Path = new PropertyPath("dic[" + id "]");
      

    但是,更优雅的解决方案是绑定整个选项卡项,如 Avneesh 的回答中所述。

    【讨论】:

      猜你喜欢
      • 2013-07-13
      • 2013-08-11
      • 2018-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多