Search code examples
c#wpflistviewtextblock

Wpf - Textblock color change column wise on random input


Currently this is my XAML Listview: i've done alot of searching but i couldn't find the solution to this, your help will be appreciated, thanks.

<ListView x:Name="listView_data" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="20,0,20,5" Width="712" Height="357">
        <ListView.View>
            <GridView x:Name="lvgvc">
                <TextBlock.Foreground>
                    <SolidColorBrush Color="black"></SolidColorBrush>
                </TextBlock.Foreground>
            </GridView>
        </ListView.View>
    </ListView>

and this is Code-Behind:

    //received data in DataTable (dt)
    for(int i=0; i < dt.Columns.Count; i++)
                    {
                        GridViewColumn gvc = new GridViewColumn();
                        gvc.Header = "Column"+i;
                        gvc.Width = 100;
                        gvc.DisplayMemberBinding = new Binding("column"+i);
                        lvgvc.Columns.Add(gvc);      
                    }

                listView_data.Items.Clear();
                listView_data.ItemsSource = dt.DefaultView;

Listview creates the columns and populates the datatTable. But the rows are completely white. I can select 6 rows in listview as there are six rows in dataTable and while debugging DataTable shows the correct data in it. How can i make the textblock color black in listview. i want textblock according to the random input column wise.


Solution

  • The Path of the DisplayMemberBinding should be set to the name of the column:

    for (int i = 0; i < dt.Columns.Count; i++)
    {
        GridViewColumn gvc = new GridViewColumn();
        gvc.Header = "Column" + i;
        gvc.Width = 100;
        gvc.DisplayMemberBinding = new Binding(dt.Columns[0].ColumnName);
        lvgvc.Columns.Add(gvc);
    }
    
    listView_data.Items.Clear();
    listView_data.ItemsSource = dt.DefaultView;
    

    And the colour of the text in the columns can be changed by defining an ItemContainerStyle in the XAML markup:

    <ListView x:Name="listView_data">
        <ListView.View>
            <GridView x:Name="lvgvc"/>
        </ListView.View>
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="Foreground" Value="Black" />
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>
    

    If you want different colours in different columns you could create a DataTemplate for each column programmatically:

    const string dataTemplate = "<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><TextBlock Text=\"{{Binding  {0}}}\" Foreground=\"{1}\" /></DataTemplate>";
    
    for (int i = 0; i<dt.Columns.Count; i++)
    {
        string colour = "Green"; ///change this one based on your colouring logic...
        DataTemplate template = System.Windows.Markup.XamlReader.Parse(string.Format(dataTemplate, dt.Columns[0].ColumnName, colour)) as DataTemplate;
        GridViewColumn gvc = new GridViewColumn() { CellTemplate = template };
        gvc.Header = "Column" + i;
        gvc.Width = 100;
        lvgvc.Columns.Add(gvc);
    }
    
    listView_data.Items.Clear();
    listView_data.ItemsSource = dt.DefaultView;