Wpf 数据绑定和绑定按钮
我有一个ItemsControl绑定到对象列表 它基本上在屏幕上显示绑定属性的列表,每个属性旁边都有一个文本框和按钮,允许您向每个数据库字段添加附加信息 在单击该按钮时,我需要获取文本框中的字符串,并将其存储在该项的对象中。XAML看起来有点像这样Wpf 数据绑定和绑定按钮,wpf,binding,Wpf,Binding,我有一个ItemsControl绑定到对象列表 它基本上在屏幕上显示绑定属性的列表,每个属性旁边都有一个文本框和按钮,允许您向每个数据库字段添加附加信息 在单击该按钮时,我需要获取文本框中的字符串,并将其存储在该项的对象中。XAML看起来有点像这样 <ItemsControl x:Name="ListDatabaseFields" ItemsSource="{Binding Path=SelectedImport.ColumnMappings}">
<ItemsControl x:Name="ListDatabaseFields" ItemsSource="{Binding Path=SelectedImport.ColumnMappings}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<StackPanel>
<TextBlock Text="{Binding DatabaseColumn.Name}" TextWrapping="Wrap" Grid.Column="0"/>
<TextBox Name="txtNewFileField" Width="100"/>
<Button Name="Add" Content="Add File Field" Style="{StaticResource LinkButton}" Width="50" Click="Add_Click"/>
</StackPanel>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
XAML中访问itemcontrol绑定到该项的ColumnMapping并更改其文件字段属性的最佳方式是什么 我不确定我是否正确理解了你的问题,如果这不是你想要的,请澄清
我已将其改编为我的测试应用程序,我的数据模板如下所示:
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/>
<TextBox MinWidth="100" Name="tbNewName"/>
<Button MinWidth="100"
Tag="{x:Reference tbNewName}" Click="ButtonNewName_Click"
Content="Do Stuff"/>
</StackPanel>
</DataTemplate>
private void ButtonNewName_Click(object sender, RoutedEventArgs e)
{
Employee emp = (sender as FrameworkElement).DataContext as Employee;
TextBox tbNewName = (sender as FrameworkElement).Tag as TextBox;
emp.Name = tbNewName.Text;
}
DataContext
是继承的,因此发送方(按钮)包含数据项,在您的示例中,列映射
,标记给我文本框
,这就是更改属性所需的全部内容
如果需要引用更多控件,可以在标记中创建数组