WPF vb.net 将不同的按钮应用于数据网格中的行
WPF vb.net Apply different button to rows in datagrid
我正在使用
在我的 DataGrid 中创建一个列
<DataGridTextColumn Header="Deploy" Binding="{Binding Deploy}" Width="100" IsReadOnly="True" CanUserSort="False"/>
在我的代码中,我使用
向网格中的列添加行
Public Structure MyRow
Public Property Deploy As String
End Structure
MyGrid.Items.Add(New MyRow With {.Deploy= "Unlimited"})
我真正想做的是,在此 Deploy 字段中,我想显示依赖于某个值的两个按钮之一。如果我的值为 0,我想显示
<Button Grid.Column="3" Padding="0" Content="A" Margin="5,8,5,12" Height="50" TextBlock.TextAlignment="Center" />
如果我有值 -1 我想显示
<Button Grid.Column="3" Padding="0" Content="B" Margin="5,8,5,12" Height="50" TextBlock.TextAlignment="Center" />
如有任何帮助,我们将不胜感激!我试过使用 CellTemplateSelector
<Page.Resources>
<DataTemplate x:Key="ATemplate">
<Button Grid.Column="3" Padding="0" Content="A" Margin="5,8,5,12" Height="50" TextBlock.TextAlignment="Center" />
</DataTemplate>
<DataTemplate x:Key="BTemplate">
<Button Grid.Column="3" Padding="0" Content="B" Margin="5,8,5,12" Height="50" TextBlock.TextAlignment="Center" />
</DataTemplate>
</Page.Resources>
...
<DataGridTemplateColumn Header="A" Width="60" CanUserSort="False" CellTemplateSelector="{StaticResource ButtonTemplateSelector}" />
...
但是我不知道如何在向 DataGrid 添加行时应用此模板
提前致谢
只需在 ButtonTemplateSelector
:
中实现您的逻辑
Public Class ButtonTemplateSelector
Inherits DataTemplateSelector
Public Property ATemplate As DataTemplate
Public Property BTemplate As DataTemplate
Public Overrides Function SelectTemplate(item As Object, container As DependencyObject) As DataTemplate
If item IsNot Nothing Then
Dim myRow = CType(item, MyRow)
If myRow.Deploy Is "0" Then
Return ATemplate
Else
Return BTemplate
End If
End If
Return MyBase.SelectTemplate(item, container)
End Function
End Class
XAML:
<DataGridTemplateColumn Header="A" Width="60" CanUserSort="False" >
<DataGridTemplateColumn.CellTemplateSelector>
<local:ButtonTemplateSelector ATemplate="{StaticResource ATemplate}"
BTemplate="{StaticResource BTemplate}" />
</DataGridTemplateColumn.CellTemplateSelector>
</DataGridTemplateColumn>
我正在使用
在我的 DataGrid 中创建一个列<DataGridTextColumn Header="Deploy" Binding="{Binding Deploy}" Width="100" IsReadOnly="True" CanUserSort="False"/>
在我的代码中,我使用
向网格中的列添加行Public Structure MyRow
Public Property Deploy As String
End Structure
MyGrid.Items.Add(New MyRow With {.Deploy= "Unlimited"})
我真正想做的是,在此 Deploy 字段中,我想显示依赖于某个值的两个按钮之一。如果我的值为 0,我想显示
<Button Grid.Column="3" Padding="0" Content="A" Margin="5,8,5,12" Height="50" TextBlock.TextAlignment="Center" />
如果我有值 -1 我想显示
<Button Grid.Column="3" Padding="0" Content="B" Margin="5,8,5,12" Height="50" TextBlock.TextAlignment="Center" />
如有任何帮助,我们将不胜感激!我试过使用 CellTemplateSelector
<Page.Resources>
<DataTemplate x:Key="ATemplate">
<Button Grid.Column="3" Padding="0" Content="A" Margin="5,8,5,12" Height="50" TextBlock.TextAlignment="Center" />
</DataTemplate>
<DataTemplate x:Key="BTemplate">
<Button Grid.Column="3" Padding="0" Content="B" Margin="5,8,5,12" Height="50" TextBlock.TextAlignment="Center" />
</DataTemplate>
</Page.Resources>
...
<DataGridTemplateColumn Header="A" Width="60" CanUserSort="False" CellTemplateSelector="{StaticResource ButtonTemplateSelector}" />
...
但是我不知道如何在向 DataGrid 添加行时应用此模板
提前致谢
只需在 ButtonTemplateSelector
:
Public Class ButtonTemplateSelector
Inherits DataTemplateSelector
Public Property ATemplate As DataTemplate
Public Property BTemplate As DataTemplate
Public Overrides Function SelectTemplate(item As Object, container As DependencyObject) As DataTemplate
If item IsNot Nothing Then
Dim myRow = CType(item, MyRow)
If myRow.Deploy Is "0" Then
Return ATemplate
Else
Return BTemplate
End If
End If
Return MyBase.SelectTemplate(item, container)
End Function
End Class
XAML:
<DataGridTemplateColumn Header="A" Width="60" CanUserSort="False" >
<DataGridTemplateColumn.CellTemplateSelector>
<local:ButtonTemplateSelector ATemplate="{StaticResource ATemplate}"
BTemplate="{StaticResource BTemplate}" />
</DataGridTemplateColumn.CellTemplateSelector>
</DataGridTemplateColumn>