我需要隐藏DataGrid中的按钮(Button1)。
按照我的代码指导我......
<StackPanel>
<WpfToolkit:DataGrid Name="grdTitle">
<WpfToolkit:DataGrid.Columns>
</WpfToolkit:DataGrid.Columns>
<WpfToolkit:DataGrid.RowDetailsTemplate>
<StackPanel x:Name="expLocalVersions">
<WpfToolkit:DataGrid Name="grdVersion">
<StackPanel>
<WpfToolkit:DataGrid.Columns>
<WpfToolkit:DataGridTemplateColumn Header="Action">
<WpfToolkit:DataGridTemplateColumn.CellTemplate>
<Button x:Name="Button1"></Button>
</WpfToolkit:DataGridTemplateColumn.CellTemplate>
</WpfToolkit:DataGridTemplateColumn>
</WpfToolkit:DataGrid.Columns>
</StackPanel>
</WpfToolkit:DataGrid>
</StackPanel>
</WpfToolkit:DataGrid.RowDetailsTemplate>
</WpfToolkit:DataGrid>
</StackPanel>
答案 0 :(得分:1)
我建议将按钮的Visible属性绑定到Itemssource的特殊属性。 像:
public Visibility ShowButton
{
get { return (OtherProperty ? Visibility.Collapsed : Visibility.Visible); }
}
和
<Button Visible="{Binding Path=ShowButton}" />
答案 1 :(得分:0)
我花了一些时间做这件事,这是一个如何做的例子
<DataGrid AutoGenerateColumns="False"
Height="287" HorizontalAlignment="Left"
Name="dataGrid1" VerticalAlignment="Top"
Width="479" Margin="12,12,0,0" CanUserAddRows="False" CanUserDeleteRows="False"
SelectionMode="Single" SelectionUnit="Cell" ColumnHeaderHeight="30" HeadersVisibility="All">
<DataGrid.Columns>
<DataGridTextColumn Header="Sr No" Binding="{Binding srNo}" IsReadOnly="True" />
<DataGridTextColumn Header="Some Title" Binding="{Binding ArticleTitle}" IsReadOnly="True" MaxWidth="290" />
<DataGridTemplateColumn MinWidth="100" Header="View">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Visibility="{Binding Display}" Content="This Is the Button" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
<强>代码强>
public partial class MainWindow : Window
{
List<ButtonDataGrid> data = new List<ButtonDataGrid>();
public MainWindow()
{
InitializeComponent();
for (int x = 0; x < 10; x++)
{
ButtonDataGrid dataToAdd = new ButtonDataGrid();
dataToAdd.srNo = (x + 1).ToString();
dataToAdd.ArticleTitle = "This is Article No " + dataToAdd.srNo;
if (x % 2 == 0)
dataToAdd.Display = System.Windows.Visibility.Visible;
else
dataToAdd.Display = System.Windows.Visibility.Hidden;
data.Add(dataToAdd);
}
dataGrid1.ItemsSource = data;
}
}
public class ButtonDataGrid
{
public string srNo { get; set; }
public string ArticleTitle { get; set; }
public Visibility Display { get; set; }
}