How can I add button to WPF GridView - c#

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>

Related

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 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"/>

wpf time consuming operations and dispatcher

I have to monitor all the frames on a network. For this, I've created 2 ListViews, one for showing the frame names, dlcs, payloads and timestamp. I have an event which fires when a new frame is received and an update is made in the frames ListView.
If i select one frame in the lv, the second ListView is populated with small information about the selected frame. Both, the selected line, and the whole second ListView are updating as that event fires and the frame payload changes.
I have only one Dispatcher.Invoke for all this work, and it seems that the GUI stays behind with the updates. Should I use two dispatchers?
One for the frame updates in the first ListView and the second for the other ListView? What priorities should I select for each of them? At this moment i am using the 6th level, named: Loaded.
<ListView ItemsSource="{StaticResource frames}"
Grid.Row="0"
Margin="0,1,0,1"
Name="lvFrames"
VerticalContentAlignment="Top"
ItemContainerStyleSelector="{StaticResource itemSelector}"
FontSize="12"
VirtualizingStackPanel.VirtualizationMode="Recycling"
VirtualizingStackPanel.IsVirtualizing="True"
BorderThickness="1"
BorderBrush="Gray"
Grid.ColumnSpan="2"
SelectionChanged="lvFrames_SelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn Header="Time" DisplayMemberBinding="{Binding Time}" Width="55"/>
<GridViewColumn Header="Dir" DisplayMemberBinding="{Binding Dir}" Width="40"/>
<GridViewColumn Header="Id" DisplayMemberBinding="{Binding Id}" Width="40"/>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="125"/>
<GridViewColumn Header="Dlc" DisplayMemberBinding="{Binding Dlc}" Width="40" />
<GridViewColumn Header="Data" DisplayMemberBinding="{Binding Data}" Width="150" />
</GridView>
</ListView.View>
</ListView>
This is the XAML for the first ListView:
<ListView ItemsSource="{StaticResource signals}"
Grid.Row="0"
Margin="0,1,-0.48,0"
Name="lvSignals"
VerticalContentAlignment="Bottom"
FontSize="12" VirtualizingStackPanel.VirtualizationMode="Recycling"
VirtualizingStackPanel.IsVirtualizing="True"
BorderThickness="1" BorderBrush="Gray" Grid.ColumnSpan="2"
VerticalAlignment="Stretch">
<ListView.View>
<GridView>
<GridViewColumn Header="Signal Name"
DisplayMemberBinding="{Binding Name}" Width="160" />
<GridViewColumn Header="Signal Value"
DisplayMemberBinding="{Binding Value}" Width="100"/>
</GridView>
</ListView.View>
</ListView>
And this one for the signals ListView.

In WPF, how to find a control from gridview?

I am using listview contain gridview to bind the data. I want to find the check box controls (i.e. chkUser) on button click event. (i.e. btnUserId click event)
How I can achieve it? If here is any other alternative solutions for the same, let me know.
My code:
<ListView Name="listUsers" SelectionMode="Single" Height="200" SelectionChanged="listUsers_SelectionChanged">
<ListView.View>
<GridView AllowsColumnReorder="true">
<GridViewColumn Header="User Id" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Name="btnUserId" Cursor="Hand" Tag="{Binding Path=UserId }" Click="btnUserId_Click" Content="{Binding Path=UserId }" Style="{StaticResource LinkButton}"></Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Name" DisplayMemberBinding="{ Binding Path=UserName }"></GridViewColumn>
<GridViewColumn Header="Email Id" DisplayMemberBinding="{ Binding Path=Email }"></GridViewColumn>
<GridViewColumn Header="Mobile" DisplayMemberBinding="{ Binding Path=Mobile }"></GridViewColumn>
<GridViewColumn Header="User Type" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<Label Name="lblUserType">User Type</Label>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Active" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="chkUser" Width="120" Content="{Binding Path=deleted }"></CheckBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Update" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Height="23" Name="button1" Tag="{Binding Path=UserId }" CommandParameter="{ Binding Path=UserId }" Click="button1_Click" Width="75">Button</Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
You should be able to use FindName to locate a ctrl by its name. In the above case it would be
listUsers.FindName("chkUser");
Cheers

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