DisplayMemberBinding when my ItemsSource is a List<string[]> - c#

How do I bind DisplayMemberBinding when my ItemsSource is a List<string[]> in my GridView?
EDIT:
Figured it out! No need to answer. (binded to [i], where i is array index)

You can use-
<ListView ItemsSource="{Binding ListOfStringArrays}">
<ListView.View>
<GridView>
<GridViewColumn Width="140" DisplayMemberBinding="{Binding [0]}" />
<GridViewColumn Width="140" DisplayMemberBinding="{Binding [1]}"/>
<GridViewColumn Width="140" Header="Column 3" DisplayMemberBinding="{Binding [2]}"/>
</GridView>
</ListView.View>
</ListView>

Related

How can I add button to WPF GridView

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:AraWPF"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<ListView x:Name="listView">
<ListView.View>
<GridView>
<GridViewColumn Header="call" DisplayMemberBinding="{Binding Call}" Width="100" />
<GridViewColumn Header="name" DisplayMemberBinding="{Binding Name}" Width="200" />
<GridViewColumn Header="path" DisplayMemberBinding="{Binding Path}" Width="100" />
<GridViewColumn Header="link" DisplayMemberBinding="{Binding Link}" Width="100" />
<GridViewColumn Header="description" DisplayMemberBinding="{Binding Description}" Width="100" />
</GridView>
</ListView.View>
</ListView>
</Grid>
</UserControl>
it's my GridView to print my data
I want to add button to GridView.
Is there any way to add button to View which is table
I'm sorry for my bad english skill.
You can use templates:
<GridView>
<GridViewColumn Header="call" DisplayMemberBinding="{Binding Call}" Width="100" />
<GridViewColumn Header="name" DisplayMemberBinding="{Binding Name}" Width="200" />
<GridViewColumn Header="path" DisplayMemberBinding="{Binding Path}" Width="100" />
<GridViewColumn Header="link" DisplayMemberBinding="{Binding Link}" Width="100" />
<GridViewColumn Header="description" DisplayMemberBinding="{Binding Description}" Width="100" />
<GridViewColumn Header="Button" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<!-- Your button using bindings, ... -->
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>

listview doesn't update on the second time only

I have a listview in WPF that doesn't add the second row until a third row is added and then all three render. Each next row renders correctly and immediately.
I don't really have a clue on why this happens only on the second time.
wpf code:
<ListView Name="DownloadList" Background="WhiteSmoke" PreviewMouseRightButtonUp="DownloadList_PreviewMouseRightButtonUp" >
<ListView.View>
<GridView>
<GridViewColumn Width="140" Header="Name" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Width="80" Header="Status" DisplayMemberBinding="{Binding Status}"/>
<GridViewColumn Width="60" Header="Size" DisplayMemberBinding="{Binding FormattedSize}"/>
<GridViewColumn Width="70" Header="Speed" DisplayMemberBinding="{Binding FormattedSpeed}"/>
<GridViewColumn Width="170" Header="Progress">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ProgressBar Height="20" Width="150" Maximum="{Binding Size}" Value="{Binding Progress}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="65" Header="Time Left" DisplayMemberBinding="{Binding FormattedTime}"/>
<GridViewColumn Width="60" Header="Peers" DisplayMemberBinding="{Binding Peers}"/>
</GridView>
</ListView.View>
</ListView>
behind code:
FileDownload download = new FileDownload(fileToDownload.id, fileToDownload.DownloadName,(int)fileToDownload.Length, partsData.Length);
wpfDownloads.Add(download);

How to display ENUM as string in ListView in XAML

I am trying to display StandardEventLevel as text, but I don't understand how to do so.
My XAML-code (Level is StandardEventLevel Enumeration):
<ListView x:Name="eventsView"
Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"
MouseDoubleClick="eventsView_MouseDoubleClick" >
<ListView.View>
<GridView>
<GridViewColumn Width="Auto"
Header="Level"
DisplayMemberBinding="{Binding Level}"/>
</GridView>
</ListView.View>
</ListView>

How to change text color of a gridViewColumn in WPF?

I have A listView In which i want to show items from dataBase. It works fine, But i want to see the items shown in cells as white in a purple listview, How to do it ?
<ListView Margin="127,114,227,357" x:Name="lv" Background="purple" >
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path=FirstName}" Header="First Name" Width="100" />
<GridViewColumn DisplayMemberBinding="{Binding Path=LastName}" Header="Last Name" Width="100" />
<GridViewColumn DisplayMemberBinding="{Binding Path=Email}" Header="Email" Width="100" />
<GridViewColumn DisplayMemberBinding="{Binding Path=Password}" Header=" Password" Width="100" />
<GridViewColumn DisplayMemberBinding="{Binding Path=Address}" Header="Address" Width="100" />
</GridView>
</ListView.View>
You need to use DataTemplate and change the text Foreground property, this is one sample for GridViewColumn.
Check on DataTemplate here: Data Templating Overview
<GridViewColumn DisplayMemberBinding="{Binding Path=FirstName}" Header="First Name" Width="1000">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="Txt" Text="{Binding FirstName}" Foreground="Purple" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
As per the accepted answer, in order for the TextBlock to remain binded and the Foreground color to change, the following worked for me:
<GridViewColumn Header="First Name" Width="1000">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="Txt" Text="{Binding Path=FirstName}" Foreground="Purple" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
and in my case I decided to create a property for the text color and bind to that
<GridViewColumn Header="First Name" Width="1000">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="Txt" Text="{Binding Path=FirstName}" Foreground="{Binding Path=TextColor}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
All wrong answers. You simply need to change GridView.ColumnHeaderTemplate property.
<ListView ItemsSource="{...}" Foreground="White" >
<ListView.View>
<GridView>
<GridView.ColumnHeaderTemplate>
<DataTemplate>
<TextBlock Foreground="White" Text="{Binding}"/>
</DataTemplate>
</GridView.ColumnHeaderTemplate>
<GridViewColumn DisplayMemberBinding="{Binding Path=FirstName}" Header="First Name" Width="1000"/>

ListViews with the same View

I have some ListView and I want that they all have the same View. So, if the following is my View:
<ListView.View>
<GridView>
<GridViewColumn Width="140" Header="Game Name"
DisplayMemberBinding="{Binding GameName}" />
<GridViewColumn Width="140" Header="Creator"
DisplayMemberBinding="{Binding Creator}" />
<GridViewColumn Width="140" Header="Publisher"
DisplayMemberBinding="{Binding Publisher}" />
</GridView>
</ListView.View>
I want something like this:
<Grid>
<ListView>
<!--Here my ListView is the same defined above-->
</ListView>
<ListView>
<!--Here my ListView is the same defined above-->
</ListView>
</Grid>
How can I accomplish this?
Define view in resources (either in resource dictionary of top-level control, or in external resource dictionary):
<StackPanel>
<StackPanel.Resources>
<GridView x:Key="myView" x:Shared="false">
<GridViewColumn Width="140" Header="Game Name"
DisplayMemberBinding="{Binding GameName}" />
<GridViewColumn Width="140" Header="Creator"
DisplayMemberBinding="{Binding Creator}" />
<GridViewColumn Width="140" Header="Publisher"
DisplayMemberBinding="{Binding Publisher}" />
</GridView>
</StackPanel.Resources>
<ListView View="{StaticResource myView}"/>
<ListView View="{StaticResource myView}"/>
</StackPanel>
Use this. Cheers
<Grid>
<ListView Name="FirstList">
<ListView.View>
<GridView>
<GridViewColumn Width="140" Header="Game Name" DisplayMemberBinding="{Binding GameName}" />
<GridViewColumn Width="140" Header="Creator" DisplayMemberBinding="{Binding Creator}" />
<GridViewColumn Width="140" Header="Publisher" DisplayMemberBinding="{Binding Publisher}" />
</GridView>
</ListView.View>
</ListView>
<ListView View="{Binding Path=View, ElementName=FirstList}" />
</Grid>

Categories