Toadums
Toadums

Reputation: 2812

Databinding and default values

What I have is a DataGrid (in WPF), and I have bound it to a list of a custom class I have made.

Lets say for simplicity sake, the class is follows:

public class MyClass{

   int val;
   string str;
   static const int alwaysSetValue = 10;

}

Is there a way to (in the databinding or the class itself) say that "if val = -1, in the datagrid instead of displaying -1, just display a blank, or ' '?

I was looking at the Binding's IsTargetNull value, and that would be good if int was a nullable type, but I would prefer to not use int? if possible.

Is there any way to do this? Some sort of override ToString() or something?

Solution see answer below. The only change I made was to set the binding & convert in code:

DataGrid.Columns.Add(new DataGridTextColumn() { Header = "Value", Binding = new Binding("val") { Converter = new MyValConverter() } });

Upvotes: 0

Views: 734

Answers (1)

kmatyaszek
kmatyaszek

Reputation: 19296

Here is example:

XAML file:

<Window x:Class="DataGridConverter.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:local="clr-namespace:DataGridConverter"
        >
    <Window.Resources>
        <local:MyValConverter x:Key="myCon" />
    </Window.Resources>
    <Grid>
        <DataGrid Name="grid" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Val" Binding="{Binding val, Converter={StaticResource myCon}}" />
                <DataGridTextColumn Header="Str" Binding="{Binding str}" />               
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

Code-behind file:

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;

namespace DataGridConverter
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            List<MyClass> _source = new List<MyClass>();
            for (int i = 0; i < 5; i++)
            {
                _source.Add(new MyClass { val = 1, str = "test " + i });
            }

            _source[2].val = -1;

            grid.ItemsSource = _source;
        }
    }

    public class MyClass
    {
        public int val { get; set; }
        public string str { get; set; }
        const int alwaysSetValue = 10;
    }

    public class MyValConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (int)value == -1 ? string.Empty : value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

Upvotes: 1

Related Questions