将文本框绑定到字典
Bind TextBox to Dictionary
我正在尝试动态创建多个 TabItem,每个选项卡都有一个文本框。然后我想将每个文本框绑定到一个字典索引。例如
TAB1 | TAB2 | TAB3
TextBox1 | TextBox2 | TextBox3
字典[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);
我应该在路径中放什么?
您可以使用视图模型动态创建它:
1.创建一个主视图模型来保持标签。
public class MainViewModel
{
private ObservableCollection<TabViewModel> tabs = new ObservableCollection<TabViewModel>();
public ObservableCollection<TabViewModel> Tabs
{
get { return this.tabs; }
}
}
创建 TabViewModel 来保存字典和 Header 来显示。在此示例中,header 是字典的键,但您可以增强它。
public class TabViewModel
{
private readonly Dictionary 词典;
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; }}
}
使用项目源创建选项卡控件:
<TabControl VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ItemsSource="{Binding Path=Tabs}" >
</TabControl>
为其指定样式
<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>
分配数据上下文:
MainViewModel mainViewModel = new MainViewModel();
字典 dictionary = 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。
这里有几点需要注意:
为了能够绑定到它,字典必须是 public 属性:
public Dictionary<string, string> dic { get; set; } = new Dictionary<string, string>();
要将绑定应用于文本框'Text
属性,您需要引用依赖项 属性 本身而不是其值:
stb.SetBinding(TextBox.TextProperty, binding);
最后,您混淆了绑定的 Source
和 Path
属性:binding.Path
是实际绑定目标所在的位置,而 Source
指的是持有这个目标的对象,在本例中是当前的class(或者,可以设置文本框'DataContext
属性,然后自动应用为绑定其所有绑定的来源):
binding.Source = this;
binding.Path = new PropertyPath("dic[" + id "]");
但是,更优雅的解决方案是绑定整个选项卡项,如 Avneesh 的回答中所述。
我正在尝试动态创建多个 TabItem,每个选项卡都有一个文本框。然后我想将每个文本框绑定到一个字典索引。例如
TAB1 | TAB2 | TAB3
TextBox1 | TextBox2 | TextBox3
字典[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);
我应该在路径中放什么?
您可以使用视图模型动态创建它: 1.创建一个主视图模型来保持标签。
public class MainViewModel
{
private ObservableCollection<TabViewModel> tabs = new ObservableCollection<TabViewModel>();
public ObservableCollection<TabViewModel> Tabs
{
get { return this.tabs; }
}
}
创建 TabViewModel 来保存字典和 Header 来显示。在此示例中,header 是字典的键,但您可以增强它。
public class TabViewModel { private readonly Dictionary 词典;
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; }} }
使用项目源创建选项卡控件:
<TabControl VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ItemsSource="{Binding Path=Tabs}" > </TabControl>
为其指定样式
<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>
分配数据上下文:
MainViewModel mainViewModel = new MainViewModel(); 字典 dictionary = 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。
这里有几点需要注意:
为了能够绑定到它,字典必须是 public 属性:
public Dictionary<string, string> dic { get; set; } = new Dictionary<string, string>();
要将绑定应用于文本框'
Text
属性,您需要引用依赖项 属性 本身而不是其值:stb.SetBinding(TextBox.TextProperty, binding);
最后,您混淆了绑定的
Source
和Path
属性:binding.Path
是实际绑定目标所在的位置,而Source
指的是持有这个目标的对象,在本例中是当前的class(或者,可以设置文本框'DataContext
属性,然后自动应用为绑定其所有绑定的来源):binding.Source = this; binding.Path = new PropertyPath("dic[" + id "]");
但是,更优雅的解决方案是绑定整个选项卡项,如 Avneesh 的回答中所述。