在单击一行数据网格上的按钮时,隐藏另一行上的按钮

时间:2019-05-09 07:55:21

标签: c# wpf

我有一个DataGrid,DataGrid的每一行包含四个按钮。enter image description here

我的屏幕看起来像这样。假设我单击“添加”的开始按钮,然后希望其他行(例如“删除”,“更新”和“删除”)上的所有按钮都隐藏起来,直到我单击“添加”的完成按钮,并在处理完成后从DataGrid中删除该“添加”行。

我的其他功能运行正常,但是唯一的问题是像单击添加行,然后单击其他行上的其他按钮一样,所以我想将其隐藏。

请帮助我解决这个问题

 <DataGrid Grid.Row="3" Grid.ColumnSpan="2" Name="gridview" Background="#b5d2fc" IsReadOnly="True" ItemsSource="{Binding}" HorizontalScrollBarVisibility="Auto"
         SelectionMode="Single" SelectionUnit="FullRow" VerticalScrollBarVisibility="Auto" Loaded="gridview_Loaded"
        CanUserDeleteRows="False" CanUserAddRows="False" CanUserResizeRows="False"  VerticalAlignment="Top"  AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn MaxWidth="155" Width="155" Binding="{Binding [Task]}" Header="Name"  />
            <DataGridTemplateColumn MaxWidth="30" MinWidth="30">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button x:Name="Start" Content="Play" Click="Button_Click" IsEnabled="{Binding IsEnableButton, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" VerticalAlignment="Top" Foreground="Black" Background="White"  Height="25"  Width="30" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn >
            <DataGridTemplateColumn MaxWidth="30" MinWidth="30">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button x:Name="Pause" Content="Pause" Click="Button_Click" IsEnabled="{Binding IsEnableButton, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" VerticalAlignment="Top" Foreground="Black" Background="White"  Height="25"  Width="30" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn MaxWidth="30" MinWidth="30">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button x:Name="Stop" Content="Stop"   Foreground="Black" Click="Button_Click" IsEnabled="{Binding IsEnableButton, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" VerticalAlignment="Top" Background="White" Height="25"  Width="30" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn MaxWidth="30" MinWidth="30">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button x:Name="Finish" Content="Finish"   Foreground="Black" Click="Button_Click" IsEnabled="{Binding IsEnableButton, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" VerticalAlignment="Top" Background="White" Height="25"  Width="30" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

这是我的xmal代码

1 个答案:

答案 0 :(得分:0)

您可以通过在按钮控件中绑定 IsEnabled 属性来实现。

xaml片段:

<DataGrid Name="MyGrid"  AutoGenerateColumns="False" Loaded="MyGrid_Loaded" Height="250"  >
<DataGrid.Columns>
    <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
    <DataGridTemplateColumn Header="B1">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <Button Content="Start" x:Name="Play" Click="Button_Click" IsEnabled="{Binding IsEnableButton, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Height="20" Width="80"/>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    <DataGridTemplateColumn Header="B2">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <Button Content="Pause"  x:Name="Pause" Click="Button_Click" IsEnabled="{Binding IsEnableButton, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Height="20" Width="80"/>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    <DataGridTemplateColumn Header="B3">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <Button Content="Stop"  x:Name="Stop" Click="Button_Click" IsEnabled="{Binding IsEnableButton, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Height="20" Width="80"/>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    <DataGridTemplateColumn Header="B4">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <Button Content="Finish"  x:Name="Finish" Click="Button_Click" IsEnabled="{Binding IsEnableButton, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Height="20" Width="80"/>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid.Columns>

xaml.cs

                public partial class MainWindow : Window
                {
                    List<TestEnableButton> objtestEnableButton = new List<TestEnableButton>();        
                    public MainWindow()
                    {
                        InitializeComponent();
                        this.DataContext = this;
                    }
                    private void MyGrid_Loaded(object sender, RoutedEventArgs e)
                    {
                        objtestEnableButton.Add(new TestEnableButton("A1", true, false));
                        objtestEnableButton.Add(new TestEnableButton("A2", true, false));
                        objtestEnableButton.Add(new TestEnableButton("A3", true, false));
                        objtestEnableButton.Add(new TestEnableButton("A4", true, false));
                        MyGrid.ItemsSource = objtestEnableButton;
                    }

                    bool butFlg = false;
                    private void Button_Click(object sender, RoutedEventArgs e)
                    {
                        var getButtonName = (sender as Button).Name;
                        var currentItem = MyGrid.CurrentItem as TestEnableButton;
                        foreach(var items in objtestEnableButton)
                        {
                            if (items.Name == currentItem.Name)
                            {
                                if (getButtonName == "Play")
                                {
                                    MessageBox.Show("Let's enjoy music...!)");
                                    butFlg = false;
                                }
                                else if (getButtonName == "Pause")
                                {
                                    MessageBox.Show("Pause the music for few minutes...!)"); butFlg = false;
                                }
                                else if (getButtonName == "Stop")
                                {
                                    MessageBox.Show("Stop the music...!)"); butFlg = false;
                                }
                                else if (getButtonName == "Finish")
                                {
                                    MessageBox.Show("End...!)");                        
                                    items.IsCheckFinish = true;
                                    butFlg = true;
                                }
                            }
                            else
                            {
                                items.IsEnableButton = false;
                            }
                        }

                        if (butFlg)
                        {
                            var itemsCollection = objtestEnableButton.RemoveAll(x => x.IsCheckFinish == true);
                            objtestEnableButton.ForEach(x => x.IsEnableButton = true);
                            MyGrid.ItemsSource = null;
                            MyGrid.ItemsSource = objtestEnableButton;
                        }            
                    }
                }
                public class TestEnableButton : INotifyPropertyChanged
                {
                    private string _name;
                    public string Name
                    {
                        get { return _name; }
                        set
                        {
                            _name = value;
                            OnPropertyChanged("Name");
                        }
                    }

                    private bool _IsEnableButton;
                    public bool IsEnableButton
                    {
                        get { return _IsEnableButton; }
                        set
                        {
                            _IsEnableButton = value;
                            OnPropertyChanged("IsEnableButton");
                        }
                    }

                    private bool _IsCheckFinish;
                    public bool IsCheckFinish
                    {
                        get { return _IsCheckFinish; }
                        set
                        {
                            _IsCheckFinish = value;
                            OnPropertyChanged("IsCheckFinish");
                        }
                    }
                    public TestEnableButton(string name, bool isB, bool isCheck)
                    {
                        Name = name;
                        IsEnableButton = isB;
                        IsCheckFinish = isCheck;
                    }

                    public event PropertyChangedEventHandler PropertyChanged;
                    public void OnPropertyChanged(string propertyName)
                    {
                        if (PropertyChanged != null)
                        {
                            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                        }
                    }
                }