【问题标题】:Using a DataGridTemplateColumn to display a button or an image使用 DataGridTemplateColumn 显示按钮或图像
【发布时间】:2014-01-16 15:49:46
【问题描述】:

我有一个我生成的邮件列表,并在 dataGrid 中显示了一些详细信息。

有些会起作用,有些会出错。

如果过程成功,我想在最后一个数据网格列中显示一个按钮,如果过程失败,则显示一个图像。

所以,我只用一个按钮就得到了这个:

 <sdk:DataGridTemplateColumn Header="Voir">
   <sdk:DataGridTemplateColumn.CellTemplate>
       <DataTemplate>
          <Button Name="btOpen" 
                  Click="btOpen_Click"
                  IsEnabled="True"/>
          </DataTemplate>
     </sdk:DataGridTemplateColumn.CellTemplate>
 </sdk:DataGridTemplateColumn>

此按钮将打开文档。

但正如我所说,某些过程可能会失败,所以我必须显示一个图像,而不是一个按钮。

我可以将图像添加到我的 datagridTemplateColumn 中,然后根据过程的成功或失败显示其中一个(很容易知道)?

如果我不能,我怎么能在这里做我想做的事?我可以不添加到不同的列吗?

谢谢。

【问题讨论】:

    标签: c# silverlight datagrid datagridtemplatecolumn


    【解决方案1】:

    您可以在按钮旁边添加图像,并将每个图像的可见性绑定到过程的成功/失败

    【讨论】:

    • 您可以将这两个块放在同一个 dataGridTemplateColumn 中。但我确实可以使用转换器更改样式和 Enable 属性。
    【解决方案2】:

    尝试在 xaml 上创建两个不同的模板。一个带有图像,一个带有按钮。然后在运行时您可以根据需要更改它。

    <Grid.Resources>
     <DataTemplate x:key="ImageDT">
        <Image>
     </DataTemplate>
    
     <DataTemplate x:key="ButtonTemplate">
        <Button>
     </DataTemplate>
    
     </Grid.Resources>
    

    然后在运行时,可能在 Load/LoadingRow 事件上

    //Here you get the row or column or cell you need
    
    if(ProcessSuccess)
       Grid.Columns["Voir"].CellTemplate = Grid.Resources["ImageDT"] ;
    else 
       Grid.Columns["Voir"].CellTemplate = Grid.Resources["ButtonTemplate"];
    

    请注意,我没有给你正确的代码,只给你想法。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-20
      • 2021-10-14
      • 1970-01-01
      • 1970-01-01
      • 2016-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多