我正在使用WPF Datagrid
,我正在尝试创建Add
按钮,而不是新行中的Update
和Delete
按钮。
以下是datagrid的代码:
<DataGrid Name="dgCust" ItemsSource="{Binding}" AutoGenerateColumns="False"
PreviewKeyDown="dgCust_PreviewKeyDown"
AddingNewItem="dgCust_AddingNewItem"
BeginningEdit="dgCust_BeginningEdit"
RowEditEnding="dgCust_RowEditEnding" >
<DataGrid.Columns>
<mui:DataGridTextColumn Header="Customer ID" Binding="{Binding ID}" IsReadOnly="True" />
<mui:DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" />
<mui:DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" />
<mui:DataGridTextColumn Header="Phone" Binding="{Binding Phone}" />
<mui:DataGridTextColumn Header="Email" Binding="{Binding Email}" />
<mui:DataGridTextColumn Header="Address" Binding="{Binding Address}" />
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ContentControl>
<ContentControl.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter" />
<local:NotBoolToVisibilityConverter x:Key="NotBoolToVisibilityConverter" />
</ContentControl.Resources>
<StackPanel Orientation="Horizontal">
<Button Content="Update" Click="Update_Row_Button_Click" CommandParameter="{Binding}"
Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}},Path=IsNewItem,Converter={StaticResource NotBoolToVisibilityConverter}}"/>
<Button Content="Delete" Click="Delete_Row_Button_Click" CommandParameter="{Binding}"
Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}},Path=IsNewItem,Converter={StaticResource NotBoolToVisibilityConverter}}"/>
<Button Content="Add" Click="Add_Row_Button_Click" CommandParameter="{Binding}"
Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}},Path=IsNewItem,Converter={StaticResource BoolToVisibilityConverter}}"/>
</StackPanel>
</ContentControl>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
还添加了名称空间:xmlns:local="clr-namespace:MyProject"
使用新行中的Update
按钮替换Delete
和Add
按钮的最佳解决方案是什么?
修改:添加了服务器端代码:
namespace MyProject
{
public class NotBoolToVisibilityConverter : BooleanToVisibilityConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (System.Convert.ToBoolean(value))
{
return Visibility.Collapsed;
}
return Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
提前致谢。
答案 0 :(得分:0)
不能使用单列显示两个按钮。这样解决方案将很容易,如下所示:
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ContentControl Loaded="ContentControl_Loaded">
<ContentControl.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter" />
<local:NotBoolToVisibilityConverter x:Key="NotBoolToVisibilityConverter" />
</ContentControl.Resources>
<StackPanel Orientation="Horizontal">
<Button Content="Update" Click="Button_Click" CommandParameter="{Binding}"
Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}},Path=IsNewItem,Converter={StaticResource NotBoolToVisibilityConverter}}"/>
<Button Content="Delete" Click="Button_Click_1" CommandParameter="{Binding}"
Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}},Path=IsNewItem,Converter={StaticResource NotBoolToVisibilityConverter}}"/>
<Button Content="Add" Click="Button_Click_2" CommandParameter="{Binding}"
Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}},Path=IsNewItem,Converter={StaticResource BoolToVisibilityConverter}}"/>
</StackPanel>
</ContentControl>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
NotBoolToVisibilityConverter 是BooleanToVisibilityConverter的反转。
添加转化器:
public class NotBoolToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (System.Convert.ToBoolean(value))
{
return Visibility.Collapsed;
}
return Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}