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>
Related
I have a class
Class Test
{
string name {get;set;}
List<string> marks {get; set;}
}
Trying to bind data of ObservableCollection to XAML
<ListView x:Name="list1" HorizontalAlignment="Left" VerticalAlignment="Top" ItemsSource="{Binding}" Width="790">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment"
Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView >
<GridViewColumn Header="Name" HeaderTemplate="{StaticResource myHeaderTemplate}" HeaderContainerStyle="{StaticResource myHeaderStyle}" Width="150">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="xyt" Text="{Binding name}" TextAlignment="Center" FontFamily="Arial" FontSize="14">
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Marks" HeaderTemplate="{StaticResource myHeaderTemplate}" HeaderContainerStyle="{StaticResource myHeaderStyle}" Width="500">
<GridViewColumn.CellTemplate>
<DataTemplate>
**<ListView x:Name="list2" ItemsSource="{Binding marks}">
<DataTemplate>
<TextBlock x:Name="marki" Text="{Binding}" TextAlignment="Center" FontFamily="Arial" FontSize="16" Margin="10" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown">
</TextBlock>
</DataTemplate>
</ListView>**
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
But I get error in binding "operation-is-not-valid-while-itemssource-is-in-use-access"
When I remove DataTemplate from list2, it shows properly with data binding, with name and corresponding listview of marks.
<ListView x:Name="list1" HorizontalAlignment="Left" VerticalAlignment="Top" ItemsSource="{Binding}" Width="790">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment"
Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView >
<GridViewColumn Header="Name" HeaderTemplate="{StaticResource myHeaderTemplate}" HeaderContainerStyle="{StaticResource myHeaderStyle}" Width="150">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="xyt" Text="{Binding name}" TextAlignment="Center" FontFamily="Arial" FontSize="14">
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Marks" HeaderTemplate="{StaticResource myHeaderTemplate}" HeaderContainerStyle="{StaticResource myHeaderStyle}" Width="500">
<GridViewColumn.CellTemplate>
<DataTemplate>
**<ListView x:Name="list2" ItemsSource="{Binding marks}">
</ListView>**
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
I am assigning datasource as
list1.ItemsSource = observablecollection<Test>;
I tried adding giving Path, relativesource, but still facing error.
I am confused why putting TextBlock causing error?
you are missing <ListView.ItemTemplate> tag around DataTemplate:
<ListView x:Name="list2" ItemsSource="{Binding marks}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock x:Name="marki" Text="{Binding}" TextAlignment="Center" FontFamily="Arial" FontSize="16" Margin="10" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown"/>
</DataTemplate>
<ListView.ItemTemplate>
</ListView>
markup
<ListView>
<DataTemplate>
</DataTemplate>
</ListView>
is equivalent to
<ListView>
<ListView.Items>
<DataTemplate>
</DataTemplate>
<ListView.Items>
</ListView>
and Items usage conflicts with setting ItemsSource.
<ListView.Items> can be omitted because Items property is chosen as ContentProperty for ListView.
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
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
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).
I'm trying add vertical lines to my grid. I have found some examples but nothing works.
<ListView ItemsSource="{Binding Path=CheckableObjectFacilities}" x:Name="ListViewObjectFacilities" Margin="5">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=IsChecked}" Click="cboxObjectFacilities_Click" HorizontalContentAlignment="Stretch"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Nazwa" >
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Category}" Header="Kategoria" >
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding EnglishName}" Header="EN" ></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Created lines in your GridView by following the example from the following site: Designer WPF.
Here is some XAML I adapted to display system font information:
<Grid>
<Grid.Resources>
<Style x:Key="MyItemContainerStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Stretch" />
</Style>
</Grid.Resources>
<ListView
ItemContainerStyle="{DynamicResource MyItemContainerStyle}"
ItemsSource="{x:Static Fonts.SystemFontFamilies}"
x:Name="myListView"
Width="Auto">
<ListView.View>
<GridView>
<GridViewColumn Header="Name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Border BorderBrush="#FF000000" BorderThickness="1,1,0,0" Margin="-6,-2,-6,-2">
<StackPanel Margin="6,2,6,2">
<TextBlock Text="{Binding Source}"/>
</StackPanel>
</Border>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Line Spacing">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Border BorderBrush="#FF000000" BorderThickness="1,1,0,0" Margin="-6,-2,-6,-2">
<StackPanel Margin="6,2,6,2">
<TextBlock Text="{Binding LineSpacing}"/>
</StackPanel>
</Border>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Sample">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Border BorderBrush="#FF000000" BorderThickness="1,1,1,0" Margin="-6,-2,-6,-2">
<StackPanel Margin="6,2,6,2">
<TextBlock FontFamily="{Binding}" FontSize="20"
Text="ABCDEFGabcdefg" />
</StackPanel>
</Border>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>