当按钮都位于 DataGridTemplateColumn 中时,从按钮中清除 TextBlock
我有一个带有 DataGridTemplateColumn 的 DataGrid。 DataGridTemplateColumn 包含一个按钮和TextBlock。 我希望按下按钮会清除 textBlock 的文本。 我该怎么做?
XAML:
<Grid>
<DataGrid ItemsSource="{Binding Persons}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}">
</DataGridTextColumn>
<DataGridTemplateColumn Header="Mask Expiration Time">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<TextBlock Text="{Binding Name}"></TextBlock>
<Button Name="btnClear" Click="btnClear_Click" >Clear</Button>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
CS 代码:
public partial class MainWindow : Window
{
public List<Person> Persons { get; set; }
public MainWindow()
{
Persons = new List<Person> { new Person { Name = "James" }, new Person { Name = "Kate" } };
DataContext = this;
InitializeComponent();
}
private void btnClear_Click(object sender, RoutedEventArgs e) {
var clearbutton = (Button) sender;
// clear the Name
}
}
public class Person
{
public string Name { get; set; }
}
I have a DataGrid with DataGridTemplateColumn.
The DataGridTemplateColumn contains a button and TextBlock.
I want that pressing the button will clear the textBlock's text.
How do I do that ?
XAML:
<Grid>
<DataGrid ItemsSource="{Binding Persons}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}">
</DataGridTextColumn>
<DataGridTemplateColumn Header="Mask Expiration Time">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<TextBlock Text="{Binding Name}"></TextBlock>
<Button Name="btnClear" Click="btnClear_Click" >Clear</Button>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
CS code:
public partial class MainWindow : Window
{
public List<Person> Persons { get; set; }
public MainWindow()
{
Persons = new List<Person> { new Person { Name = "James" }, new Person { Name = "Kate" } };
DataContext = this;
InitializeComponent();
}
private void btnClear_Click(object sender, RoutedEventArgs e) {
var clearbutton = (Button) sender;
// clear the Name
}
}
public class Person
{
public string Name { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用Button继承的
DataContext
:Use the inherited
DataContext
of the Button:我建议使用
Command
来代替,并通过CommandParameter
属性传递当前的 Person 对象。像这样的事情:那么您需要做的就是设置对象的属性(并更新绑定,因为它看起来不像 Person 实现 INotifyPropertyChanged )
I would suggest using a
Command
instead and passing the current Person object via theCommandParameter
property. Something like this:then all you'd need to do is set the property of the object (and update the binding, since it doesn't look like Person implements
INotifyPropertyChanged
)您可以使用
ICommand
:然后在 XAML 中:
You could use an
ICommand
:Then in the XAML: