In WPF, how to find a control from gridview? - c#

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

Related

WPF: ListView.SelectedItem when Cell has ScrollBar

I have a problem in WPF.
So, double click works good with simple cells. But column, where I use ScrollBar doesnt react. How it is possible to handle those cells?
Here is my code:
<ListView>
<GridView>
<GridViewColumn Header="ColumnWithScroll" IsStretch="True">
<GridViewColumn.CellTemplate>
<DataTemplate DataType="SimpleItems">
<ScrollViewer HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Auto"
Name="Scroller" MaxHeight="200">
<TextBlock Text="{Binding TextBind}">
</TextBlock>
</ScrollViewer>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Header" Width="150">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Smth}"
TextWrapping="WrapWithOverflow"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
<ListView.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{Binding SomeCommand}"/>
</ListView.InputBindings>
</ListView>
Solution:
Just set the property Focucable="False" of ScrollViewer

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

How to Style a cell in Gridview under ListView

How to style every cell in Gridview.I want my cell text in a rounded text box with red background.My code is
<ListView Margin="0" ItemsSource="{Binding ParamList}" >
<ListView.View >
<GridView >
<GridView.ColumnHeaderTemplate>
<DataTemplate>
<Border BorderThickness="2" CornerRadius="5" Height="75" MinWidth="60" >
<Border.Background>Gray</Border.Background>
<TextBlock Foreground="WhiteSmoke" Margin="5"
Text="{Binding}" Width="Auto"
HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
</DataTemplate>
</GridView.ColumnHeaderTemplate>
<GridViewColumn Header="category1" DisplayMemberBinding="{Binding Path=param1}" />
<GridViewColumn Header="category2" DisplayMemberBinding="{Binding Path=param1}" />
</GridView>
</ListView.View>
</ListView>
Use the GridViewColumn's CellTemplate property to set a DataTemplate containing a TextBox with the appropriate styling for each of your columns. I don't believe there is a way to set it at the Gridview level so that you don't have to set it on each column individually.
<Grid>
<ListView ItemsSource="{Binding Items}">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Background="Red" Text="{Binding Item1}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Item2}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</Grid>

Textbox not shown in listView

I'm trying to get a textbox to be shown in a column of listview that also uses GridView.
<ListView Margin="30,120,0,0" Name="EquipmentView" SelectionChanged="EquipmentView_SelectionChanged" ItemsSource="{Binding}" Foreground="Black" FontWeight="Normal" FontFamily="Segui Bold" HorizontalAlignment="Left" Width="329" Height="100" VerticalAlignment="Top">
<ListView.View>
<GridView>
<GridViewColumn Header="Control" Width="175" DisplayMemberBinding="{Binding Path=Control}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=ControlBlock}" Width="Auto" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Setting" Width ="150" DisplayMemberBinding="{Binding Path=Setting}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=SettingText}" Width="Auto" GotFocus="TextBox_GotFocus" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
But when I execute the above, I sit the listview as a grid, but I can't see or edit the textbox. Any help will be greatly appreciated. Thanks in advance.
Remove the DisplayMemberBinding since you've specified a template for that column (both columns, actually).

Categories