我有一个Movie
类Dim _characters = New ObservableCollection(of String)
在我的 MainWindow.vb 中,
Dim movies = New ObservableCollection(of Movie)
Me.parentG.DataContext = Me.movies
我希望在运行时根据按钮点击为电影添加字符 如何获得点击了角色按钮的特定电影?
MainWindow.xaml: -
<Grid Name="parentG" >
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="700"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Text="{Binding MovieName}" />
<Button Grid.Column="1" Content="New Character" Click="newCharacterButton_Click" Height="20" Width="100" HorizontalAlignment="Left" />
<ListBox Grid.Column="2" Name="cList" ItemsSource="{Binding Characters}">
<ListBox.ItemTemplate >
<DataTemplate >
<TextBox Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
答案 0 :(得分:0)
终于明白了。以下是步骤: -
Button
Grid
DataContext
,将其转换为(在本例中),Movie
Movie
中获取此Movie array
的索引,假设每个Movie
都存在索引属性,并在初始化期间设置。character
movie
醇>