I'm making an WPF-application that has a databound ListView. When loading the application, the width of the ListViewColumns will auto-resize, but when adding or changing an item it doesn't auto-resize. I've tried refreshing the listview, setting the width of the column to auto, -1 or -2 in both xaml and VB-code and I've tries changing the itemssource to nothing before refilling it with items. This is the xaml code:
<ListView x:Name="lsvPersons" Margin="5,5,5,35" ItemsSource="{Binding Persons}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name"/>
<GridViewColumn DisplayMemberBinding="{Binding Gender}" Header="Gender"/>
<GridViewColumn DisplayMemberBinding="{Binding Age}" Header="Age"/>
</GridView>
</ListView.View>
</ListView>
<Button x:Name="btnAddPerson" Content="Add" Height="25" Margin="0,0,200,5" Width="80"/>
The binding works with a controller, which gets the persons with Person.getPersons from a SQL-database:
Private oController As New MainController()
Public Sub New()
MyBase.New()
Me.InitializeComponent()
Me.DataContext = oController
End Sub
After pressing the button, a window will open to add a person. After the window is closed, the item is added to the listview with following code, which refreshes the items in the listview:
lsvPersons.ItemsSource = Person.getPersons()
So, what do I need to do to auto-resize the columns of the listview when an item is added or editted?
GridView gv = lvSrchResulsGrid.View as GridView;
if (gv != null)
{
int colNum = 0;
foreach (GridViewColumn c in gv.Columns)
{
// Code below was found in GridViewColumnHeader.OnGripperDoubleClicked() event handler (using Reflector)
// i.e. it is the same code that is executed when the gripper is double clicked
// if (adjustAllColumns || App.StaticGabeLib.FieldDefsGrid[colNum].DispGrid)
// if (adjustAllColumns || fdGridSorted[colNum].AppliedDispGrid)
// {
if (double.IsNaN(c.Width))
{
c.Width = c.ActualWidth;
}
c.Width = double.NaN;
// }
}
}
Related
I have a function which updates every 5 seconds. I would like to keep the selected item but the item does not get selected.
The function below updates every 5 seconds and updates the list in the list view:
C#
public void festJSONUpdateEventHandler()
{
var tempfest = Workspace.This.festStats.Selectedfest;
//REFRESH BINDINGS HERE!!
Workspace.This.festStats.festItems = MainWindow._fest.festData.fest_Items;
Workspace.This.festStats.Selectedfest = tempfest;
}
XAML
<DataTemplate>
<StackPanel Orientation="Vertical">
<ListView x:Name="lvfest" ItemsSource="{Binding festItems}" SelectedItem="{Binding Selectedfest, Mode=TwoWay}" IsSynchronizedWithCurrentItem="True" >
<ListView.View>
<GridView>
<GridViewColumn Width="100" DisplayMemberBinding="{Binding id}" >
<GridViewColumn.Header>
<GridViewColumnHeader Tag="ID" Click="lvfestColumnHeader_Click">ID</GridViewColumnHeader>
</GridViewColumn.Header>
</GridViewColumn>
<GridViewColumn Width="100" DisplayMemberBinding="{Binding formatType}" >
<GridViewColumn.Header>
<GridViewColumnHeader Tag="Format" Click="lvfestColumnHeader_Click">Format</GridViewColumnHeader>
</GridViewColumn.Header>
</GridViewColumn>
<GridViewColumn Width="100" DisplayMemberBinding="{Binding modifiedIso8601}" >
<GridViewColumn.Header>
<GridViewColumnHeader Tag="Date" Click="lvfestColumnHeader_Click">Date</GridViewColumnHeader>
</GridViewColumn.Header>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</DataTemplate>
More C#
class festViewModel : ToolViewModel
{
public festItem _selectedfest;
public festItem Selectedfest
{
get { return _selectedfest; }
set
{
if (_selectedfest != value)
{
_selectedfest = value;
RaisePropertyChanged("Selectedfest");
}
}
}
private List<festItem> _festItems;
public List<festItem> festItems
{
get { return _festItems; }
set
{
if (_festItems != value)
{
_festItems = value;
RaisePropertyChanged("festItems");
}
}
}
}
From comments:
I have a library that extracts JSON data and in there, there is a list
of objects called fest_Items. from this list, I send the objects to
festItems list in the festViewModel which is bound to my list view.
Is the list being recreated at each function call (ergo 5 seconds)? It may be because your items do not contains the same reference to the object.
Two or more objects sharing the same values may be not the same references. If you are recreating the list, that may be the cause of the binding failing to retreive the item.
As a solution, I suggest finding an unique property (like the ID property of your model) and search the item in the newly created list.
Exemple:
public void festJSONUpdateEventHandler()
{
var tempfestID = Workspace.This.festStats.Selectedfest.ID;
//REFRESH BINDINGS HERE!!
Workspace.This.festStats.festItems = MainWindow._fest.festData.fest_Items;
// Find the first festStat that has the same ID as the old selected one.
Workspace.This.festStats.Selectedfest = Workspace.This.festStats.FirstOrDefault(x => x.ID == tempfestID);
}
Let me know if it is not working.
I've tried almost all the soulutions provided on stackoverflow and can't seem to get it to work. Am new to wpf and mvvm and was trying to bind a datatable to a listview and below is my code.
//code for viewmodel
public DataTable RetrieveDetails
{
get
{
DataTable users = new DataTable();
string dataBaseName = "name.db3";
using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + dataBaseName + "; Version=3;"))
{
var details = "SELECT * FROM users";
connection.Open();
SQLiteCommand cmd = new SQLiteCommand(details, connection);
cmd.ExecuteNonQuery();
SQLiteDataAdapter adapter = new SQLiteDataAdapter(details, connection);
adapter.SelectCommand.CommandTimeout = 120;
DataSet ds = new DataSet();
adapter.Fill(ds);
return ds.Tables[0];
}
}
}
And below is how i bind the datatable to my list view
<ListView x:Name="FormOneView" ItemsSource="{Binding Path=RetrieveDetails}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding FirstName}" Header="First Name" />
</GridView>
</ListView.View>
</ListView>
I know this is supposed to be simple but am having a hard time displaying the data.
Try this approach:
<ListView x:Name="FormOneView" ItemsSource="{Binding}" DataContext="{Binding Path=allData}" HorizontalAlignment="Left" Height="100" Margin="243,289,0,0" VerticalAlignment="Top" Width="191">
<ListView.View>
<GridView>
<GridViewColumn Header="Time" Width="50" DisplayMemberBinding="{Binding Path=Tid}"/>
<GridViewColumn Header="Acceleration" Width="70" DisplayMemberBinding="{Binding Path=Acceleration}"/>
</GridView>
</ListView.View>
</ListView>
in this XAML I'm giving you an example of a datatable with two columns. I'm binding my ListView columns to my sql table column headers using DisplayMemberBinding.
Your backgournd code is actually fine, but you need to bind the DataContext of your ListView using propertyChangedEventHandler. Below is an example of how to do such a binding:
Add following two lines in your method:
adapter.Fill(ds, "users");
allData = ds.Tables["users"].DefaultView;
Then add following methods in your class. Your class should inherit INotifyPeropertyChanged
(yourClassName : INotifyPeropertyChanged) otherwise it won't work.
private DataView _allData;
public DataView allData
{
get { return _allData; }
set
{
if (value != _allData)
{
_allData = value;
OnPropertyChanged("allData");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
the result:
in order for the above code to work, need to add the following line of code
listview.datacontext = class holding alldata.allData
I am having problem binding ObservableCollection as ItemsSource to a combo box (this combobox is in a listview of having some rows).
I followed A collection of StackPanel as ItemsSource of ComboBox but I did not get any clues for resolving my problem.
Problem:
I was able to add items to a combobox which is at the top on the form.
I have created a listview containing 3 text blocks and 1 combobox.
I am successful in populating data for the text blocks in listview.
But the problem lies with Combobox. First time, it shows all the items for each row in ListView. Once I select item or click on the combobox again to see the items, my list disappears. Only one combobox in the listview row shows all items. Other comboboxes shows blank.
Also, I was trying to save the index of the selected item and show the selected panel next time. But I did not get how to bind the stackpanel with selecteditem and selecteditemvalue.
I tried many ways of directly binding the items to the combobox in listview. But nothing worked. Request someone to help me on this.
Details of the code is given below:
I have XAML like below:
<Grid>
<Grid Height="40">
<ComboBox x:Name="cbList" />
</Grid>
<Grid Margin="0,56,0,168"></Grid>
<ListView x:Name="lvFirst" Margin="0,195,0,12">
<ListView.View>
<GridView >
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Width="50" x:Uid="tbListView1" Text="{Binding FirstName}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Width="50" x:Uid="tbListView2" Text="{Binding LastName}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Width="50" x:Uid="tbListView1" Text="{Binding ID}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox Width="100" x:Uid="cbListView" ItemsSource="{Binding Path=SPForComboBox}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
In the code behind I have a Contact class as below:
public class Contact : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
void Notify(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
private string _fn;
private string _ln;
public string FirstName
{
get
{ return _fn; }
set
{ _fn = value; Notify("FirstName"); }
}
public string LastName
{
get
{ return _ln; }
set
{ _ln = value; Notify("LastName"); }
}
private int _id;
public int ID
{
get { return _id; }
set { _id = value; Notify("ID"); }
}
public StackPanel sp;
public override string ToString()
{
return FirstName + " " + LastName;
}
private ObservableCollection<StackPanel> _sp;
public ObservableCollection<StackPanel> SPForComboBox
{
get { return _sp; }
set { _sp = value; Notify("SPForComboBox"); }
}
}
To populate cbList Items, I am repeatedly calling below function after Initialization() function:
private void AddColours(string name, byte hexcolor)
{
//Add this ShiftType to Combo box
SolidColorBrush rectangleBrush = new SolidColorBrush();
Color color = new Color();
color.A = hexcolor;
rectangleBrush.Color = color;
System.Windows.Controls.StackPanel stkPanel = new StackPanel(); //stack panel to hold rectangle + text
stkPanel.Orientation = Orientation.Horizontal;
cbList.Items.Add(stkPanel);
Rectangle colorRect = new Rectangle(); //rectangle showing colour for shift
colorRect.Height = 12;
colorRect.Width = colorRect.Height;
colorRect.Fill = rectangleBrush;
stkPanel.Children.Add(colorRect);
System.Windows.Controls.TextBlock cboText = new TextBlock(); //Name of shift
cboText.Text = name;
cboText.Margin = new Thickness(5, 5, 5, 5);
stkPanel.Children.Add(cboText);
}
In the main window class, I have a created an ObservableCollection object as public static (object name is contacts).
public static ObservableCollection<Contact> contacts = new ObservableCollection<Contact>();
After the Initialization() function, I am populating contacts as below:
AddColours("First", 100);
AddColours("Second", 50);
AddColours("Third", 20);
AddColours("Fourth", 0);
AddColours("Fifth", 80);
Contact c1 = new Contact();
c1.FirstName = "Digo";
c1.LastName = "Maradona";
c1.ID = 0;
c1.SPForComboBox = new ObservableCollection<StackPanel>();
foreach (StackPanel sp in cbList.Items)
{
c1.SPForComboBox.Add(sp);
}
Contact c2 = new Contact();
c2.FirstName = "Brian";
c2.LastName = "Lara";
c2.ID = 1;
c2.SPForComboBox = new ObservableCollection<StackPanel>();
foreach (StackPanel sp in cbList.Items)
{
c2.SPForComboBox.Add(sp);
}
Contact c3 = new Contact();
c3.FirstName = "Sachin";
c3.LastName = "Tendulkar";
c3.ID = 2;
c3.SPForComboBox = new ObservableCollection<StackPanel>();
foreach (StackPanel sp in cbList.Items)
{
c3.SPForComboBox.Add(sp);
}
contacts.Add(c1);
contacts.Add(c2);
contacts.Add(c3);
lvFirst.ItemsSource = contacts;
HighCore, Thank you very much for the links. I have existing implementation and adding combobox to that.
I too felt that the method followed is not good. I shall certainly look at the alternatives provided by you and suggested by pushpraj.
Answer:
I thought referring objects in other combobox will work till the items exist in that combobox. But I need to create rectangle and textblock for reach combobox I am creating and for each entry in that combobox.
So certainly I need to do it in foreach loop.
Also, once I do this I can use SelectedIndex referring to the integer and SelectedItem to sp (individual stackpanel in that class).
This method is not good to follow but might be helpful for somebody.
Thanks.
I have the following in my dgridtext.xaml file.
XAML
<Grid x:Name="grid1">
<StackPanel>
<ListView Name="listview1" IsTextSearchEnabled="True" TextSearch.TextPath="Enquiry_Number">
<ListView.View>
<GridView ColumnHeaderToolTip="Multiple Category Information">
<GridViewColumn DisplayMemberBinding="{Binding Path=Enquiry_Number}" Header="Enquiry number"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=Consignee_Ref}" Header="Consignee reference"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=Booking_Reference}" Header="Booking reference"/>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</Grid>
Something like this.
dgridtest.xaml.cs
for (int i = 0; i < listview1.Items.Count; i++)
{
MessageBox.Show(listview1.Items[i].ToString());
}
But all that returns is System.Data.DataRowView
Please help.
Update:
In My dgridtextxaml.cs file, I call the DataManager.cs class passing a dataset object which is the source of my listview(listview1).
DataManager.BindFilteredData(dts);
listview1.ItemsSource = dts.Tables[0].DefaultView;
And this is what I have in my DataManager.cs class
public static void BindFilteredData(DataSet dts)
{
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString))
{
string sql = "SELECT Enquiry_Number, Consignee_Ref, Booking_Reference FROM ConsHead";
using (SqlDataAdapter adapter = new SqlDataAdapter(sql, connection))
{
adapter.Fill(dts);
}
}
}
You can cast the item to whatever class you use in the ListView and then use any property you need from it. Something like:
var item = listView1.Items[i] as YourClassHere;
Edit
In your case, since you bind the ItemsSource to a DataSet directly you probably can use DataRowView class, and then use the properties from it:
var firstItem = listview1.Items[0] as DataRowView;
var firstCellValue = firstItem.Row[0];
I saw ICollectionView being introduced with WPF to handle situations when you need sorting and filtering enabled. I even saw few articles which sort items, but my main concern is why my approach is failing. Lets see my code :
<ListView ItemsSource="{Binding}" x:Name="lvItems" GridViewColumnHeader.Click="ListView_Click">
<ListView.View>
<GridView AllowsColumnReorder="True">
<GridViewColumn Header="Id" DisplayMemberBinding="{Binding Id}" />
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Developer">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Developer}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Salary">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Salary}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
In codebehind, when the Item is clicked I am doing like this :
ICollectionView Source { get; set; }
private void ListView_Click(object sender, RoutedEventArgs e)
{
GridViewColumnHeader currentHeader = e.OriginalSource as GridViewColumnHeader;
if(currentHeader != null && currentHeader.Role != GridViewColumnHeaderRole.Padding)
{
//using (this.Source.DeferRefresh())
//{
SortDescription currentPropertySort = this.Source.SortDescriptions.FirstOrDefault<SortDescription>(item => item.PropertyName.Equals(currentHeader.Column.Header.ToString()));
if (currentPropertySort != null)
{
if (currentPropertySort.Direction == ListSortDirection.Ascending)
currentPropertySort.Direction = ListSortDirection.Descending;
else
currentPropertySort.Direction = ListSortDirection.Ascending;
}
else
this.Source.SortDescriptions.Add(new SortDescription(currentHeader.Column.Header.ToString(), ListSortDirection.Ascending));
//}
this.Source.Refresh();
this.lvItems.DataContext = this.Source;
this.lvItems.UpdateLayout();
}
}
So whenever the header for the ListBox is clicked, the item need to be sorted. I am holding the collection using a property called Source and then using it by calling lvItems.DataContext = this.Source. But the code does not seem to be working.
Here's an updated version of your ListView_Click method that somewhat works. I'm not sure exactly what sorting behavior you were looking for but the version below "stacks up" a set of sort descriptions, making the last clicked column as the "primary sort description". I hope this makes sense and I hope the code below helps. =)
private void ListView_Click(object sender, RoutedEventArgs e)
{
GridViewColumnHeader currentHeader = e.OriginalSource as GridViewColumnHeader;
if(currentHeader != null && currentHeader.Role != GridViewColumnHeaderRole.Padding)
{
if (this.Source.SortDescriptions
.Count((item) => item.PropertyName.Equals(currentHeader.Column.Header.ToString())) > 0)
{
SortDescription currentPropertySort = this.Source
.SortDescriptions
.First<SortDescription>(item => item.PropertyName.Equals(currentHeader.Column.Header.ToString()));
//Toggle sort direction.
ListSortDirection direction =
(currentPropertySort.Direction == ListSortDirection.Ascending)?
ListSortDirection.Descending : ListSortDirection.Ascending;
//Remove existing sort
this.Source.SortDescriptions.Remove(currentPropertySort);
this.Source.SortDescriptions.Insert(0, new SortDescription(currentHeader.Column.Header.ToString(), direction));
}
else
{
this.Source.SortDescriptions.Insert(0, new SortDescription(currentHeader.Column.Header.ToString(), ListSortDirection.Ascending));
}
this.Source.Refresh();
}
}
EDIT:
By the way, one of the problems in your code above is your call to "FirstOrDefault" to query an existing SortDescription. See, SortDescription is a struct, which is non-nullable so the call to FirstOrDefault will never be null and will always return an instance. Therefore, the "else-statement" in your code above will never get called.