I Have three objects in a combo box when they are selected the items are listed in the List View. The items are added but there is no text inside the rows like the names of the objects. and when I remove the ListView.View from my Xaml the items displays "(Collection)"
//ComboBox Binding
public void BindComboBox()
{
using (DataClassesDataContext DC = new DataClassesDataContext())
{
cbItem.Items.Clear();
foreach (tblProduct R in DC.tblProducts)
cbItem.Items.Add(R);
cbItem.DisplayMemberPath = "ProductName";
}
//Combobox selectionChanged
List<PIDData> items = new List<PIDData> { };
IEnumerable<PIDData> query = items.Where(item => item.PName != null);
lvDataBinding.Items.Add(items);
txt1.Text = query.ToString();
//Xaml
<ListView Margin="338,20,1,16" Name="lvDataBinding">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding PName}">
<GridViewColumnHeader Tag="Product Name" Width="100">Product Name</GridViewColumnHeader>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<ComboBox x:Name="cbItem" HorizontalAlignment="Left" Margin="13,315,0,0" VerticalAlignment="Top" Width="321" SelectionChanged="cbItem_SelectionChanged"/>
Well what you are doing is that you are adding the LINQ Query result as "one object" to your list view. So the list view is not dealing with each piddata but with one "IEnumable".
So either you are adding them one by one or you change to assignment of ItemsSource.
Related
I'm creating a windows WPF interface for an XML API of a Music catalog.
So in creating the list of albums, songs, playlists, etc, I created list boxes with columns (with visual studio 'cause I have no experience on windows programming):
<Window x:Class="AmpacheWPF.Programacion"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:AmpacheWPF"
mc:Ignorable="d"
Title="Programacion" Height="917.833" Width="1206.666">
<Grid>
<ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="26,39,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="comboBox_SelectionChanged"/>
<ListView x:Name="albums" HorizontalAlignment="Left" Height="146" Margin="26,99,0,0" VerticalAlignment="Top" Width="549" SelectionChanged="albums_SelectionChanged">
<ListView.View >
<GridView AllowsColumnReorder="False">
<GridView.ColumnHeaderContextMenu>
<ContextMenu/>
</GridView.ColumnHeaderContextMenu>
<GridViewColumn Header="Album"
DisplayMemberBinding="{Binding Album}" Width="120"/>
<GridViewColumn Header="Artist"
DisplayMemberBinding="{Binding Artist}"/>
<GridViewColumn Header="Year"
DisplayMemberBinding="{Binding Year}"/>
</GridView>
</ListView.View>
</ListView>
<Label x:Name="label" Content="Label" HorizontalAlignment="Left" Margin="160,54,0,0" VerticalAlignment="Top"/>
<ListView x:Name="Songs" HorizontalAlignment="Left" Height="257" Margin="26,286,0,0" VerticalAlignment="Top" Width="549" SelectionChanged="Songs_SelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn Header="#"
DisplayMemberBinding="{Binding Pista}"/>
<GridViewColumn Header="Cancion"
DisplayMemberBinding="{Binding Titulo}"/>
<GridViewColumn Header="Compositor"
DisplayMemberBinding="{Binding Compositor}"/>
<GridViewColumn Header="Artista"
DisplayMemberBinding="{Binding Artist}"/>
<GridViewColumn Header="Tiempo"
DisplayMemberBinding="{Binding Tiempo}"/>
<GridViewColumn Header="Genero"
DisplayMemberBinding="{Binding Genero}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
It has Columns (Album, Artist, Year)
All the info is auto-filled with the XML API of my music server.
I have a combo box with Genres (Latin, Salsa, etc)
Every time I click a genre it loads the info in the ListView albums, and when I click an album it fills the ListView songs.
What I'm trying to do with no success is to fix the columns' size (that is the only thing I can do with "width") and want the columns to expand vertically when the text doesn't fit on the column width (like Microsoft excel does) (so I have a 2 or 3 line rows) but please tell me where to put it on the code because really I have no experience programming on windows.
Here is some example of my code in case it helps:
public Programacion()
{
InitializeComponent();
serverurl = Loginwindow.serverurl;
string ampuser = Loginwindow.ampuser;
token = Loginwindow.token;
//obteniendo lista de tags
string tagurl = "http://" + serverurl + "/server/xml.server.php?action=tags&auth=" + token;
string[] tagarray = { tagurl, "1", "tag", "name", "albums" };//Creando array con datos a utilizar
tagsinfo = xmlcs2.Xmlparser(tagarray);//Llamando afuncion que lee XML
//Obteniendo total de espacios musicales contando el array
totalgenres = tagsinfo.GetUpperBound(0);
//Lennado ComboBox1
int g = Programacion.totalgenres;
for (int y = 0; y < g; y++)
{
comboBox.Items.Add(tagsinfo[y][1]); //
}
}
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int index = comboBox.SelectedIndex;
id = tagsinfo[index][0];
string totalalbums = tagsinfo[index][2];
//obteniendo lista de albums
string tagurl = "http://" + serverurl + "/server/xml.server.php?action=tag_albums&auth=" + token + "&filter=" + id;
string[] tagarray = { tagurl, totalalbums, "album", "name", "tracks", "artist", "year" };//Creando array con datos a utilizar
albumsinfo = xmlcs2.Xmlparser(tagarray);//Llamando afuncion que lee XML
//ordenando alfabeticamente
Sort(albumsinfo, 1);
//Lennado ListView Albums
albums.ItemsSource = null;
int g = Convert.ToInt32(totalalbums);
List<Albumlist> items = new List<Albumlist>();
for (int y = 0; y < g; y++)
{
if (albumsinfo[y][4] == "0")
{
items.Add(new Albumlist() { Album = albumsinfo[y][1], Artist = albumsinfo[y][3], Year = "" });
}
else
{
items.Add(new Albumlist() { Album = albumsinfo[y][1], Artist = albumsinfo[y][3], Year = albumsinfo[y][4], Id = albumsinfo[y][0] });
}
}
albums.ItemsSource = items;
}
Thanks in advance for your help.
It looks like what you need to do is change out the DisplayMemberBinding in your GridView for CellTemplate as per this answer, which will allow you to customize the presenter object into a custom TextBlock with wrapping (this will then also do the right thing with regards to letting the grid layout know it needs more space).
You might not get Excel-behavior right away, and you may need to play around with the other TextBlock settings to get it to work exactly how you want.
So I am having an issue where I have a GridView's ItemsSource bound to a collection of object. I also have a column of Check boxes that can be used to select object the user wishes to remove and all related items. The problem I am having is that when the user selects one item, I get stuck in a loop of the items being continually selected. Does anyone have an idea on how I can stop the programmatic selection of these check boxes from firing the Checked event.
Property in use:
List<MyObject> _localCollection = new List<MyObject>();
List<MyObject> LocalCollection
{
get { return _localCollection; }
set
{
_localCollection = value;
OnPropertyChanged("LocalCollection");
}
}
Loose example of XML code:
<GridView Name="grdItems">
<GridViewColumn>
<GridViewColumn.Header>
<CheckBox/>
</GridViewColumn.Header>
<!--Column Template-->
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Tag="{Binding ObjID}"
IsChecked="{Binding ToRemove, Mode=OneWay}"
Checked="SelectRelative" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
The "SelectRelative" method looks as follows:
private void SelectRelative(object sender, RoutedEventArgs e)
{
Dispatcher.BeginInvoke((Action)(() =>
{
//Get the Object Id we need
int selectedId = Convert.ToInt32(((CheckBox)sender).Tag);
//Get all objects that share this ID
List<MyObjects> objLst = new List<MyObjects>(((IEnumerable<MyObjects>)grdItems.ItemsSource));
//Clear the local collection property of our items used in the items source
LocalCollection.Clear();
//Remove the items source since we are updating it
grdItems.ItemsSource = null;
//Go through each item in the list and if the object id's match select them to remove
foreach(var item in objLst)
{
if(item.ObjId == selectedId)
item.ToRemove = true;
//Add the object to our property
LocalCollection.Add(item);
}
//Re-establish the item source with our new collection
grdItems.ItemsSource = LocalCollection;
}));
}
try this:
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox x:Name="chkboxPaid" Checked="chkboxPaid_Checked" Content="check" Style="{StaticResource MaterialDesignCheckBox}" IsChecked="{Binding Path=IsSelected, Mode=TwoWay,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type DataGridRow}}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
I mean how should I display my datastructure for I can work with it like with table?
I want to have a table with rows and columns can be dynamicaly added and removed, but in the rest is should looks like a table.
Now it's represented like IList>, because I should resize it, as I said before. But now i want to display it in DataGrid, and be able to have rows, coulmns, and work with "Cells". But i cannot bind it properly, it it does not display anything, only an empty cell for every row.
What should I do? Or mby use arrays and resize them after each row/column addition?
Please, advice.
Now I have this one:
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
var storage = new Storage();
storage.Data.Add(new DataRow("Bla-bla")
{
new DataEntity() {Text = "bla-bla", Forces = new[] {ForceEnum.AA}},
new DataEntity() {Text = "bla-bla", Forces = new[] {ForceEnum.UA}}
});
DataListView.DataContext = new StorageModel(storage);
}
public class StorageModel
{
public StorageModel()
{
}
public StorageModel(IStorage storage)
{
DataRowList = new ObservableCollection<DataRow>(storage.Data);
}
public ObservableCollection<DataRow> DataRowList
{
get;
set;
}
}
public class DataRow : IList<DataEntity>
{
public string Name { get; private set; }
private readonly List<DataEntity> _list = new List<DataEntity>();
...
_
<ListView ItemsSource="{Binding DataRowList}" Name="DataListView">
<ListView.ItemTemplate>
<DataTemplate>
<ListView ItemsSource="{Binding}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
i want be able to create something similar to this, but with 2-way binding...
You seem to be asking for TreeView and not ListView since you have a tree alike data structure.
If you do not want to use a TreeView I would suggest you to use a simple ListView with a small trick. That is you could explose an Expander which will add or remove items underneath parent with an indent to fake tree look and still all cells in column would be resized together.
Btw dont forgot to define columns
Here is how:
<ListView Margin="10" Name="lvUsers">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
<GridViewColumn Header="Mail" Width="150" DisplayMemberBinding="{Binding Mail}" />
</GridView>
</ListView.View>
</ListView>
If you wish to implement that expander trick behavior I would suggest you to listen to event OnExpanded and add or remove the needed rows.
C# WPF
I have a list view with 3 columns in it. What I want to happen is when I double click on a row, it will capture the values of the columns for the selected row and store them as variables so I can pass them off to another page or simply manipulate them as needed. What I have here is my practice code. For right now, I am just populating a message box to see if I can get the variables out correctly. What is happening is it is not grabbing anything from the row. I figured this out by removing the if block and saw that string name = selectedObject.ilName; was null. an additional question I have is in regards to the statement ingListLV.SelectedItems[0] as InventoryList the [0], what exactly does that refer to? I initally thought it referred to the values it returned, so [0] would be the value in column 1, [1] would be column 2 etc. but i know that is wrong.
Here is my XAML
<ListView x:Name="ingListLV" HorizontalAlignment="Center" Height="100" Margin="0,145,0,0" VerticalAlignment="Top" Width="Auto"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
SelectedValuePath="InventoryName"
Style="{DynamicResource listViewStyle}"
MouseDoubleClick="Open_Edit">
<ListView.View>
<GridView>
<GridViewColumn x:Name="InvName" Width="100" Header="Name" DisplayMemberBinding="{Binding Path=InventoryName}" />
<GridViewColumn Width="50" Header="Qty" DisplayMemberBinding="{Binding Path=Qty}" />
<GridViewColumn Width="50" Header="Type" DisplayMemberBinding="{Binding Path=Type}" />
</GridView>
</ListView.View>
</ListView>
and my code behind
private void Open_Edit(object sender, RoutedEventArgs e)
{
var selectedObject = ingListLV.SelectedItems[0] as InventoryList;
if (selectedObject == null)
{
return;
}
string name = selectedObject.ilName;
MessageBox.Show(name);
}
public class InventoryList
{
public string ilName { get; set; }
public decimal ilQty { get; set; }
public string ilType { get; set; }
}
EDIT
Here is where i am loading data in to the listview
private void LoadLV()
{
auroraDataEntities = new AuroraDataEntities();
ObjectQuery<Inventory> inventories = auroraDataEntities.Inventories;
//Returns only opjects with a quantity greater than 0, so it won't show anything you are out of
var fillList = from q in inventories
where q.Qty > 0
select q;
ingListLV.ItemsSource = fillList.ToList();
}
In ListView.SelectionMode Single (which is default) use SelectedItem not SelectedItems.
var selectedObject = ingListLV.SelectedItem as Inventory;
The [0] refers to the first selected item (row) in a multiple selection.
I have a ListView with 3 headers, declared in XAML as follows:
<ListView Name="myListView">
<ListView.View>
<GridView>
<GridViewColumn Header="H1"/>
<GridViewColumn Header="H2"/>
<GridViewColumn Header="H3"/>
</GridView>
</ListView.View>
</ListView>
I want to programmatically add a ListViewItem to this ListView, being able to set the content within the ListViewItem that will go under the first, second, and third columns individually. So far, I have only gotten this far:
ListViewItem l = new ListViewItem();
l.Content = "Content";
myListView.Items.Add(l);
This sets each column to the string "Content". How can I change the code above so that I can add a ListViewItem that will display "Content 1", "Content 2", and "Content 3" under the first, second, and third columns respectively? I tried to look for a SubItem or ListViewSubItem property within ListViewItem, but found nothing.
I assume there's a simple solution, but maybe I'm wrong. Please do not mention data binding, because I only want an answer to the question of programmatically setting the Content property to reflect individual changes in each column.
Thank you very much.
This is not databinding. Think of the Binding statement as giving the column a name.
<ListView Name="myListView">
<ListView.View>
<GridView>
<GridViewColumn Header="H1" DisplayMemberBinding="{Binding Col1}"/>
<GridViewColumn Header="H2" DisplayMemberBinding="{Binding Col2}"/>
<GridViewColumn Header="H3" DisplayMemberBinding="{Binding Col3}"/>
</GridView>
</ListView.View>
</ListView>
In code:
myListView.Items.Add(new { Col1 = "test1", Col2 = "Test2", Col3="test3"});
In winforms here is what you do.
You have to set the column headins first, otherwise nothing will show. The add the list view items using a string array.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
listView1.View=View.Details;
//Set Columns
listView1.Columns.Add("Col1");
listView1.Columns.Add("Col2");
listView1.Columns.Add("Col3");
//Fill Rows
ListViewItem lvi;
lvi=new ListViewItem(new string[] { "A", "B", "C" });
listView1.Items.Add(lvi);
lvi=new ListViewItem(new string[] { "D", "E", "F" });
listView1.Items.Add(lvi);
lvi=new ListViewItem(new string[] { "G", "H", "I" });
listView1.Items.Add(lvi);
}
}
a screenshot of the result is