0
我有了这个XAML代码:DataTemplate中为TextBlock的和ComboBox
<Window x:Class="New_app_2.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window2" Height="400" Width="500">
<Grid>
<StackPanel Orientation="Horizontal">
<ItemsControl ItemsSource="{Binding TestList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding }"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<ComboBox ItemsSource="{Binding Tags}" VerticalAlignment="Top">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}"></TextBlock>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
</Grid>
</Window>
我必然的TextBlock的财产TestList:
private List<string> _testList;
public List<string> TestList
{
get
{
return new List<string>() { "Test1", "Test2", "Test3", "Test4", "Test5" };
}
set
{
_testList = value;
}
}
和一个ComboBox属性标签(看起来几乎与TestList属性相同,但具有不同的字符串)。
我的目标是显示,在这样的形式:
测试1 [标签组合框]
的Test2 [标签组合框]
Test3的[标记组合框]
TEST4 [标签组合框]
TEST5 [标签组合框]
相反,我越来越:
Test1的[标签组合框]
的Test2
Test3的
TEST4
TEST5
后来,我也喜欢能够获取每个TestList元素选择哪些选项的信息。
也许这很容易,但我刚开始学习WPF。
它的工作原理!但我真的不明白。 –
Loreno
另一件事:我需要做什么才能阅读用户在每个ComboBox中选择的内容? – Loreno
阅读['Binding' class](https://msdn.microsoft.com/en-us/library/system.windows.data.binding(v = vs.110).aspx)及其属性/相关的课程,这非常重要。还有[此概述](https://msdn.microsoft.com/en-us/library/vstudio/ms752347(v = vs.100).aspx)。在这里,您将绑定源更改为文档元素,因为当前的'DataContext'(这是隐式源)已更改为当前的模板项目。因为源代码现在是一个元素,您需要专门在绑定路径中获取'DataContext'。 –