如何在数据网格的特定行上启用特定按钮

时间:2018-05-11 13:44:03

标签: c# wpf wpfdatagrid

我想为Button的特定行启用DataGrid,并为后面的代码中的其他行禁用它。问题是IsEnabled属性会影响所有行中的所有按钮,这不是我想要的。它不能有两个不同的值(某些行可见,而其他行同时折叠)

你能帮我吗?

这是我的xaml:

<DataGrid ItemsSource="{Binding TaskUsers}" Name="DgUser" AutoGenerateColumns="False"  IsReadOnly="True" SelectionUnit="FullRow" SelectionMode="Single" CanUserAddRows="False" CanUserSortColumns="False" SelectedValuePath="TaskUserId" >
   <DataGrid.Columns>
       <DataGridTemplateColumn>
             <DataGridTemplateColumn.HeaderTemplate>
                   <DataTemplate>
                        <Button  BorderThickness="0" ToolTip="Subscribe" Click="AddTaskUser_OnClick">
                            <Image Source="/Sinergi;component/Images/add-icon16.png"/>
                        </Button>
                   </DataTemplate>
             </DataGridTemplateColumn.HeaderTemplate>
             <DataGridTemplateColumn.CellTemplate>
                   <DataTemplate>
                        <Button BorderThickness="0" ToolTip="Delete a User" Click="DeleteTaskUser_OnClick" Name="DeleteUserButton" Loaded="DeleteUserButton_OnLoaded">
                              <Image Source="/Sinergi;component/Images/Trash-can-icon16.png" />
                        </Button>
                   </DataTemplate>
             </DataGridTemplateColumn.CellTemplate>
      </DataGridTemplateColumn>

      <DataGridTextColumn Header="UserName" Binding="{Binding User.Name}"  />
  </DataGrid.Columns>

这是代码背后的代码

private void DeleteUserButton_OnLoaded(object sender, RoutedEventArgs e)
    {
        var delete = sender as Button;
        if (delete == null) return;
        foreach (var item in DgUser.Items)
        {
            var o = (TaskUser) item;
            if (o.UserId == Identity.This.OnBehalfOfUser.UserId) continue;
            delete.IsEnabled = false;
            delete.Visibility = Visibility.Collapsed;
        }
    }

0 个答案:

没有答案