Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从DataGridTemplateColumn.CellTemplate访问按钮_C#_Wpf_Data Binding - Fatal编程技术网

C# 如何从DataGridTemplateColumn.CellTemplate访问按钮

C# 如何从DataGridTemplateColumn.CellTemplate访问按钮,c#,wpf,data-binding,C#,Wpf,Data Binding,我为DataGrid提供了以下.xaml,并希望在.cs代码中的特定条件下显示/隐藏按钮 <DataGridTemplateColumn Header="Action" Width="auto" > <DataGridTemplateColumn.CellTemplate> <DataTemplate> <StackPanel Orientation="Horizontal">

我为DataGrid提供了以下.xaml,并希望在.cs代码中的特定条件下显示/隐藏按钮

<DataGridTemplateColumn Header="Action" Width="auto" >
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Button x:Name="btnConfirm" Content="Confirm" Click="ConfirmButton_Click"  Height="auto" Width="auto"  Opacity="100" Background="Transparent" BorderBrush="Transparent" HorizontalAlignment="Left"/>
                <Button x:Name="btnDecline" Content="Decline" Click="btnDecline_Click" Height="auto" Width="auto"  Opacity="100" Background="Transparent" BorderBrush="Transparent" HorizontalAlignment="Left" />
                <Button x:Name="btnCancel" Content="Cancel" Click="btnCancel_Click" Height="auto" Width="auto"  Opacity="100" Background="Transparent" BorderBrush="Transparent" HorizontalAlignment="Left"/>
            </StackPanel>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

您必须为此使用绑定。所以,让我们采取一种快速的方法:

以下是这方面的代码:

public partial class MainWindow : Window
{
    public bool ButtonIsVisible
    {
        get { return (bool)GetValue(ButtonIsVisibleProperty); }
        set { SetValue(ButtonIsVisibleProperty, value); }
    }

    // Using a DependencyProperty as the backing store for ButtonIsVisible.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ButtonIsVisibleProperty =
        DependencyProperty.Register("ButtonIsVisible", typeof(bool), typeof(MainWindow), new UIPropertyMetadata(true));

    public ObservableCollection<Person> items { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        items = new ObservableCollection<Person>();
        items.Add(new Person() { Name = "FirstName" });
        items.Add(new Person() { Name = "SecondName" });

        this.DataContext = this;
    }
}
public class BoolToVis : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var isVisible = (bool)value;

        return isVisible ? Visibility.Visible : Visibility.Hidden;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
您的可见性不是布尔类型,因此我们需要一个转换器:

public partial class MainWindow : Window
{
    public bool ButtonIsVisible
    {
        get { return (bool)GetValue(ButtonIsVisibleProperty); }
        set { SetValue(ButtonIsVisibleProperty, value); }
    }

    // Using a DependencyProperty as the backing store for ButtonIsVisible.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ButtonIsVisibleProperty =
        DependencyProperty.Register("ButtonIsVisible", typeof(bool), typeof(MainWindow), new UIPropertyMetadata(true));

    public ObservableCollection<Person> items { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        items = new ObservableCollection<Person>();
        items.Add(new Person() { Name = "FirstName" });
        items.Add(new Person() { Name = "SecondName" });

        this.DataContext = this;
    }
}
public class BoolToVis : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var isVisible = (bool)value;

        return isVisible ? Visibility.Visible : Visibility.Hidden;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
下面是整个XAML代码:

<Window x:Class="DataGridCellsBackground.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    xmlns:local="clr-namespace:DataGridCellsBackground"        >
<Grid>
    <Grid.Resources>
        <local:BoolToVis x:Key="BoolTovisibilityConverter"/>
    </Grid.Resources>
    <DataGrid SelectionUnit="Cell" 
              ItemsSource="{Binding items}" 
              AutoGenerateColumns="False">
        <DataGrid.Resources>
        </DataGrid.Resources>
        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Name}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="Visible" Visibility="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}, Path=DataContext.ButtonIsVisible, Converter={StaticResource BoolTovisibilityConverter}}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>


事实上,您在按钮可见性中看到了一种奇怪的绑定语法。为什么呢?我假设您在模型中不需要这个功能,所以我回到了DataGrid的DataContext,以达到这个DependencyProperty。我不能完全确定您的场景,但我已经向您展示了一些易于使用的机制。

我已经编辑了您的标题。请参阅“”,其中的共识是“不,他们不应该”。可能重复