2
Hello, I am new with XAML/WPF and I have the following problem. I have a DataGrid
which receives as Itemssource a ObservableCollection<DataEstoque>()
thus
public class DataEstoque
{
public string Id { get; set; }
public string Descricao { get; set; }
public int WebCodigo { get; set; }
}
In the view, my Datagrid is declared as such:
<DataGrid x:Name="DataGridProdutos" AutoGenerateColumns="False" ColumnWidth="Auto"
Margin="10" Background="White" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="75"
SortDirection="Ascending"/>
<DataGridTextColumn Header="Descrição do produto" Width="*"
Binding="{Binding Descricao}" />
<DataGridTemplateColumn Width="auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DockPanel Margin="0, -3, 0, -3">
<Button Click="VincularProduto" DockPanel.Dock="Left" Foreground="Green"
Style="{DynamicResource BtnLink}" Content="vincular"
Width="60" Height="24" Tag="{Binding Id}"
Visibility="{Binding WebCodigo, Source=WebCodigo,
Converter={StaticResource VisibleConverter},
ConverterParameter=WebCodigo,
FallbackValue=Visible}"/>
<Button Click="AtualizarProduto" DockPanel.Dock="Left" Foreground="Blue"
Style="{DynamicResource BtnLink}" Content="atualizar"
Width="60" Height="20" Tag="{Binding Id}"
Visibility="{Binding WebCodigo, Source=WebCodigo,
Converter={StaticResource VisibleConverter},
ConverterParameter=WebCodigo,
FallbackValue=Collapsed}"/>
</DockPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
The intention is that when I carried the ObservableCollection<DataEstoque>
i can, based on the convert I did or did not show the buttons but it does not work. The convert was made like this:
public class VisibleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
CultureInfo culture)
{
if ((value is string && string.IsNullOrEmpty((string)value)) ||
(value is bool && !!(bool)value) ||
(value is int && ((int)value) > 0) ||
(value != null))
return Visibility.Visible;
return null;
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture) =>
throw new NotImplementedException();
}
The converter code has already been tested, it checks the type of the sent object and, based on the references it returns Visible
or null. If returned null, it will use the FallbackValue
. The problem is, I can’t do the automatic treatment for each line on the datagrid, and it’s not smart to loop and force the visualization of one or another datagrid button.
Does anyone know how to solve?