I have this model:
Public Data
{
string name;
string age;
string id;
DateTime time;
}
Currently each ListView row contains all this 4 peoperties:
<ListView.View>
<GridViewColumn Width="20" DisplayMemberBinding="{Binding Index}" />
<GridViewColumn Width="320" DisplayMemberBinding="{Binding Description}" />
<GridViewColumn Width="150" DisplayMemberBinding="{Binding IPAddress}" />
<GridViewColumn Width="150" DisplayMemberBinding="{Binding time}" />
</GridView>
</ListView.View>
Now i want to make this ListView Width smaller so i want to each Row will contain 2 columns with 2 properties on each of them.
Is it possible ?
You need to override the default CellTemplate of GridViewColumn inorder to display your results inside a ListView.View
<GridViewColumn >
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Index}" />
<TextBlock Text="{Binding Description}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn >
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding IPAddress}" />
<TextBlock Text="{Binding time}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
Don't use a ListView
Use a ListBox DataTemplate with a Grid
Related
I have this GridViewColumn:
<GridViewColumn Width="180" Header="Status" DisplayMemberBinding="{Binding Status}"/>
Now i want to be able to change this Column text color also via code behind.
I try:
<GridViewColumn Width="180" Header="Status" DisplayMemberBinding="{Binding Status}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="Txt" Text="{Binding Status}" Foreground="Yellow" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
But it seems that the text color is not changing.
Since you already specifying the celltemplate for each item, you don't need to include the DisplayMemberBinding property, just remove it
<GridViewColumn Width="180" Header="Status">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="Txt" Text="{Binding Status}" Foreground="Yellow" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
I created a listview. I need to get the user's input from the Textbox inside the listview. When user click on submit button User input from listview are displayed in a MessageBox. How to get user input from textbox inside the listview
<ListView x:Name="lstvQualification"
Height="96"
Margin="10,6,14,0"
VerticalAlignment="Top">
<ListView.View>
<GridView>
<GridViewColumn Width="249" Header="Education">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox x:Name="txtEducation"
Width="247"
Text="{Binding education}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="253" Header="College/Institution">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox x:Name="txtCollege"
Width="251"
Text="{Binding college}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="88" Header="Mark(%)">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox x:Name="txtMark"
Width="86"
Text="{Binding mark}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="88" Header="Add">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button x:Name="btnAddQualification"
Click="btnAddQualification_Click"
Content="Add"
Style="{StaticResource NewImg}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Button Content="Submit" x:name="submit" />
This could be the example for change in your code to work fine:
<ListView x:Name="myListView"
HorizontalAlignment="Stretch"
ItemsSource="{Binding Source={StaticResource myCollectionViewSource},XPath='Party',Mode=TwoWay}">
<ListView.View>
<GridView>
<GridViewColumn Width="100" DisplayMemberBinding="{Binding XPath='Contact'}" Header="Contact"/>
<GridViewColumn DisplayMemberBinding="{Binding XPath='Qty'}" Header="Q"/>
<GridViewColumn DisplayMemberBinding="{Binding XPath='Amount'}" Header="Amt"/>
<GridViewColumn x:Name="tbTot" Header="Tot">
<GridViewColumn.CellTemplate>
<DataTemplate>
<DockPanel>
<TextBox LostFocus="TextBox_LostFocus" Width="100" Text="{Binding XPath='Text'}" />
</DockPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
You already use {Binding ...} everywhere in the cell templates, those bindings are TwoWay by default, so any user input would/should automatically be passed to underlying data objects. However, I don't see any binding on ListView.ItemsSource property, so most probably you forgot to prepare the dataobjects. Create a collection of dataitems, bind them to the ItemsSource, let the ListView display them and celltemplates with bindings should update the dataitems' properties with no extra work from your side. When submit is pressed, just check the collection of dataitems, all the data entered should be already there (assuming you made all models and bindings properly).
I am trying to create a DataTemplate that can be shared for all columns of a GridView, which has it's columns created dynamically (through code-behind).
I would like to create the DataTemplate as a resource in XAML instead of entirely in code-behind, but I can't figure out how to get the bindings to work properly.
The following is the closest I could come up with (but does not work):
<DataTemplate x:Key="ListViewCellTemplate">
<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type GridViewColumn}}}" />
</DataTemplate>
This template is assigned as the CellTemplate of each column as follows:
BindableDataTable table = this.DataContext as BindableDataTable;
foreach (BindableDataColumn c in table.Columns)
{
GridViewColumn col = new GridViewColumn();
col.Header = c.ColumnName;
col.CellTemplate = this.FindResource("ListViewCellTemplate") as DataTemplate;
v.Columns.Add(col);
}
Answer:
Set a DataTemplate in your resources
<UserControl.Resources>
<ResourceDictionary>
<DataTemplate x:Key="GridViewCellTemplateStyle">
<TextBlock Text="{Binding}">
<TextBlock.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{Binding DataContext.CommandDoubleClick, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}"/>
</TextBlock.InputBindings>
</TextBlock>
</DataTemplate>
</ResourceDictionary>
</UserControl.Resources>
Create your grid view and make the columns inherit this datatemplate
<ListView>
<ListView.View>
<GridView>
<GridViewColumn Width="Auto" Header="Column1" CellTemplate="{StaticResource GridViewCellTemplateStyle}"/>
<GridViewColumn Width="Auto" Header="Column2" CellTemplate="{StaticResource GridViewCellTemplateStyle}"/>
<GridViewColumn Width="Auto" Header="Column3" CellTemplate="{StaticResource GridViewCellTemplateStyle}"/>
<GridViewColumn Width="Auto" Header="Column4" CellTemplate="{StaticResource GridViewCellTemplateStyle}"/>
</GridView>
</ListView.View>
</ListView>
This exmaple shows you how to create a double clickable row in a GridViewColumn.
Just change the DataTemplate as you see fit
I'm having the exact same problem.
I want to be able to apply the data template to different columns but have each column bound to a separate data field.
<ListView ItemsSource="{Binding}" Name="listViewIMS" Grid.Row="1" Margin="0,0,0,4" FontSize="11" AlternationCount="2" SelectionMode="Extended">
<ListView.Resources>
<DataTemplate x:Key="Templ">
<TextBlock TextAlignment="Left" Text="{Binding}"/>
</DataTemplate>
<DataTemplate x:Key="Tempr">
<TextBlock Width="78" Margin="-6,0" TextAlignment="Right" Text="{Binding}" />
</DataTemplate>
<Style x:Key="HeaderStyleRight" TargetType="GridViewColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Right"/>
</Style>
<Style x:Key="HeaderStyleLeft" TargetType="GridViewColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Left"/>
</Style>
</ListView.Resources>
<ListView.View>
<GridView x:Name="gridViewInvoices">
<GridViewColumn Width="80" DisplayMemberBinding="{Binding Document}">
<GridViewColumnHeader Tag="docNo" Content="Document" />
</GridViewColumn>
<GridViewColumn Width="220" DisplayMemberBinding="{Binding Customer}">
<GridViewColumnHeader Tag="customer" Content="Customer"/>
</GridViewColumn>
<GridViewColumn Width="100" DisplayMemberBinding="{Binding inv_date, StringFormat='dd/MM/yy'}" >
<GridViewColumnHeader Tag="date" Content="Date" />
</GridViewColumn>
<GridViewColumn Width="100" DisplayMemberBinding="{Binding inv_l_catalogue}" >
<GridViewColumnHeader Tag="catalogue" Content="Item" />
</GridViewColumn>
<GridViewColumn Width="60" DisplayMemberBinding="{Binding inv_l_qty}" >
<GridViewColumnHeader Tag="qty" Content="Qty" />
</GridViewColumn>
<GridViewColumn Width="80" DisplayMemberBinding="{Binding inv_l_price, StringFormat='0.00' }" >
<GridViewColumnHeader Tag="unitPrice" Content="Price" />
</GridViewColumn>
<GridViewColumn Width="50" DisplayMemberBinding="{Binding inv_l_per}" >
<GridViewColumnHeader Tag="per" Content="Per" />
</GridViewColumn>
<GridViewColumn Header="Goods" CellTemplate ="{StaticResource Tempr}" HeaderContainerStyle="{StaticResource HeaderStyleRight}" Width="80" >
<!--GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Width="78" Margin="-6,0" TextAlignment="Right" Text="{Binding inv_l_lgoods2, StringFormat='0.00'}" />
</DataTemplate>
</GridViewColumn.CellTemplate>-->
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
#jonathan-beresford
This question is old... But, for future reference:
You can binding on the template
<DataTemplate x:Key="Templ">
<TextBlock TextAlignment="Left" Text="{Binding inv_l_catalogue}"/>
</DataTemplate>
i have a list view with the following code
<ListView Name="configurationModeGrid" Width="400" Margin="236,440,58,143" BorderBrush="Black" FontFamily="Microsoft Sans Serif" FontSize="11">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Width="110" Header="Network">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Margin="-5,0,-5,0" Name="NetworkName" Height="20" Width="108" Text="{Binding NetworkName, Mode=OneWay}" Background="#FFFFCAA6" HorizontalAlignment="Left" Style="{StaticResource DisableText}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="110" Header="Trend Mode">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox Name="TrendListItems" Margin="-5,0,-5,0" Width="108" ItemsSource="{Binding TrendModeList}" SelectedItem="{Binding CurrentMode}" HorizontalAlignment="Left" Style="{StaticResource ListBoxCombo}" SelectionChanged="TrendListItems_SelectionChanged" >
</ComboBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="LAN" Width="50">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Name="LanNumber" Margin="-5,0,-5,0" Width="48" Text="{Binding LanNumber,Mode=OneWay}" IsEnabled="False" Background="#FFFFCAA6" HorizontalAlignment="Left" Style="{StaticResource DisableText}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Label" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Name="LanLabel" Margin="-5,0,-5,0" Width="98" Text="{Binding LanLabel, Mode=OneWay}" Background="#FFFFCAA6" HorizontalAlignment="Left" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
The dropdown list will bind the following options
i) internetwork
II) lan
iii) none
Question
if the lan item in dropdownlist is selected from the row one of list view, i would like to make other other dropdownlist item's item must become none.
Can this be done using WPF UI or have to use only data model for this?
why is this displaying date and time (rather than just date)?
<ListView Name="AttachmentsLB" Width="160" ItemsSource="{Binding Path=PicuAttachments}" >
<ListView.View>
<GridView>
<GridViewColumn Header="Start" DisplayMemberBinding="{Binding StartDate}" Width="80">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=., StringFormat=d}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Finish" DisplayMemberBinding="{Binding FinishDate}" Width="80">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=., StringFormat=d}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
I have also tried StringFormat = {}{0:d} and StringFormat = \{0:d\} with no success
problem of StringFormat on the wrong Binding
<GridViewColumn Header="Start" DisplayMemberBinding="{Binding StartDate, StringFormat=d}" Width="75">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
How about using this? Change the StringFormat in your binding :
DisplayMemberBinding="{Binding Path=startDate, StringFormat='yyyy-MM-dd'}"