当按钮都位于 DataGridTemplateColumn 中时,从按钮中清除 TextBlock

发布于 2024-11-09 01:25:26 字数 1785 浏览 4 评论 0原文

我有一个带有 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 ?

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑梦风尘 2024-11-16 01:25:26

使用Button继承的DataContext

var person = (sender as FrameworkElement).DataContext as Person;
person.Name = String.Empty;

Use the inherited DataContext of the Button:

var person = (sender as FrameworkElement).DataContext as Person;
person.Name = String.Empty;
以歌曲疗慰 2024-11-16 01:25:26

我建议使用 Command 来代替,并通过 CommandParameter 属性传递当前的 Person 对象。像这样的事情:

<Button Content="Clear" 
        Command="{Binding DataContext.ClearNameCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" 
        CommandParameter="{Binding}" />

那么您需要做的就是设置对象的属性(并更新绑定,因为它看起来不像 Person 实现 INotifyPropertyChanged )

I would suggest using a Command instead and passing the current Person object via the CommandParameter property. Something like this:

<Button Content="Clear" 
        Command="{Binding DataContext.ClearNameCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" 
        CommandParameter="{Binding}" />

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)

挽清梦 2024-11-16 01:25:26

您可以使用 ICommand

public class ClearNameCommand : ICommand
{
    public bool CanExecute(object parameter, IInputElement target)
    {
        var person = parameter as Person;
        return (person != null && person.Name.Length > 0);
    }

    public void Execute(object parameter, IInputElement target)
    {
        var person = parameter as Person;
        if (person != null)
        {
            person.Name = String.Empty;
        }
    }
}

然后在 XAML 中:

<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
    <TextBlock Text="{Binding Name}"></TextBlock>
    <Button x:Name="btnClear"
            Command="{StaticResource ClearCommand}"
            CommandParameter="{Binding}">Clear</Button>
</StackPanel>

You could use an ICommand:

public class ClearNameCommand : ICommand
{
    public bool CanExecute(object parameter, IInputElement target)
    {
        var person = parameter as Person;
        return (person != null && person.Name.Length > 0);
    }

    public void Execute(object parameter, IInputElement target)
    {
        var person = parameter as Person;
        if (person != null)
        {
            person.Name = String.Empty;
        }
    }
}

Then in the XAML:

<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
    <TextBlock Text="{Binding Name}"></TextBlock>
    <Button x:Name="btnClear"
            Command="{StaticResource ClearCommand}"
            CommandParameter="{Binding}">Clear</Button>
</StackPanel>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文