I would like to be able to select an item that is bound to my gridview and press the delete button, which would delete the item from the observable collection to which the gridview is bound to.
<ListView Height="166" HorizontalAlignment="Left" Margin="12,290,0,0" Name="listView1" VerticalAlignment="Top" Width="636" ItemsSource="{Binding SearchTerms}" KeyUp="SearchTermsGrid_KeyPressed">
<ListView.View>
<GridView x:Name="SearchTermsGrid">
<GridViewColumn Width="155" Header="Search Term" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Width="90" Header="Status" DisplayMemberBinding="{Binding Status}"/>
<GridViewColumn Width="60" Header="Count" DisplayMemberBinding="{Binding NumberToDownload}"/>
<GridViewColumn Width="60" Header="Size" DisplayMemberBinding="{Binding Size}"/>
<GridViewColumn Width="60" Header="Type" DisplayMemberBinding="{Binding Type}"/>
</GridView>
</ListView.View>
</ListView>
ObservableCollection<SearchTerm> _searchTerms = new ObservableCollection<SearchTerm>();
public ObservableCollection<SearchTerm> SearchTerms
{
get
{
return _searchTerms;
}
}
private void SearchTermsGrid_KeyPressed(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete)
{
//I dunno, this might be a bad approach.
}
}
does this not work:?
this._searchTerms.Remove(this.listView1.SelectedItem);
Related
Hi all i want to bind my listbox depends on combobox selected item here is my code
<StackPanel Orientation="Horizontal" >
<ComboBox Name="cmbID" Width="150" Margin="10" Height="30" SelectedItem="{Binding CmbSelected,Mode=TwoWay}" DisplayMemberPath="ID" ItemsSource="{Binding MyStudent,Mode=TwoWay}"/>
<Button Name="btnGetDetail" Margin="10" Command="{Binding getDetails}" Content="Get Details" Height="30" Width="90"/>
<TextBox Name="tbName1" Width="90" Height="30" Text="{Binding ElementName=cmbID,Path= SelectedItem.Sub}"></TextBox>
</StackPanel>
In Above code i am binding my combobox to one observable collection and want to bind my ListView to selected item of combobox below is my code
<ListView Name="myStudent" ItemsSource="{Binding CmbSelected,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Width="420" Height="150">
<ListView.View >
<GridView x:Name="grdStudentDetails">
<GridViewColumn Header="ID" DisplayMemberBinding="{Binding ElementName=cmbID,Path=SelectedItem.ID}" Width="30"/>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding ElementName=cmbID,Path =SelectedItem.Name}" Width="100"/>
<GridViewColumn Header="RollNum" DisplayMemberBinding="{Binding ElementName=cmbID,Path=SelectedItem.RollNum}" Width="100"/>
<GridViewColumn Header="Subject" DisplayMemberBinding="{Binding ElementName=cmbID,Path=SelectedItem.Sub}" Width="100"/>
<GridViewColumn Header="PhNumber" DisplayMemberBinding="{Binding ElementName=cmbID,Path=SelectedItem.PhNum}" Width="100"/>
</GridView>
</ListView.View>
</ListView>
I am not able to find where I am doing mistake even I bind my text box with same binding it is working fine. please refer textbox just below my combobox's XAML.
my viewmodle.cs code is as follow
private student cmbSelcted;
public student CmbSelected
{
get { return cmbSelcted; }
set { cmbSelcted = value; OnPropertyChanged("CmbSelected"); }
}
public ObservableCollection<student> MyStudent
{
get { return myStudent; }
set { myStudent = value; OnPropertyChanged("MyStudent"); }
}
Can't see the ItemsSource of your ListView! Hope you've bind it to an collection. If not - try this approach.
<ListView Grid.Row="1" ItemsSource="{Binding SelectedStudents, Mode=OneWay}">
<ListView.View >
<GridView x:Name="grdStudentDetails">
<GridViewColumn Header="ID" DisplayMemberBinding="{Binding Id}" Width="30"/>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="100"/>
<GridViewColumn Header="RollNum" DisplayMemberBinding="{Binding RollNum}" Width="100"/>
<GridViewColumn Header="Subject" DisplayMemberBinding="{Binding Sub}" Width="100"/>
<GridViewColumn Header="PhNumber" DisplayMemberBinding="{Binding PhNum}" Width="100"/>
</GridView>
</ListView.View>
</ListView>
ViewModel
private Student _cmbSelected;
public Student CmbSelected
{
get { return _cmbSelected; }
set
{
_cmbSelected = value;
if (_cmbSelected != null)
{
SelectedStudents = new List<Student>() { _cmbSelected };
}
else
{
SelectedStudents = new List<Student>();
}
OnPropertyChanged();
}
}
private List<Student> _selectedStudents;
public List<Student> SelectedStudents
{
get { return _selectedStudents; }
set
{
_selectedStudents = value;
OnPropertyChanged();
}
}
Hope you see that it's important to have an ItemsSource on your ListView.
<Grid>
<StackPanel>
<ListView x:Name="usergrid" Margin="100,50,100,0" HorizontalAlignment="Center" FontSize="20" ItemsSource="{Binding Path=user1}">
<ListView.View>
<GridView>
<GridViewColumn Header="UserId" DisplayMemberBinding="{Binding UserId,Mode=TwoWay}" Width="100" ></GridViewColumn>
<GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName,Mode=TwoWay}" Width="150" />
<GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName,Mode=TwoWay}" Width="150" />
<GridViewColumn Header="City" DisplayMemberBinding="{Binding City,Mode=TwoWay}" Width="150" />
<GridViewColumn Header="State" DisplayMemberBinding="{Binding State,Mode=TwoWay}" Width="150" />
<GridViewColumn Header="Country" DisplayMemberBinding="{Binding Country,Mode=TwoWay}" Width="150" />
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</Grid>
I would like to resize columns of a grid view into ListView to contents. There is the XAML code:
<ListView x:Name="listViewStatEntries"
AlternationCount="2" Grid.Row="8" Grid.ColumnSpan="3" Background="White"
ItemsSource="{Binding StatResult.StatEntries}" >
<ListView.View>
<GridView>
<GridViewColumn Width="Auto" DisplayMemberBinding="{Binding Param.Speciality}">
<GridViewColumn.Header>
<GridViewColumnHeader Content="{Binding Labels[2851]}" Tag="Param.Speciality" Click="listViewStatEntriesColumnHeader_Click" Width="Auto" />
</GridViewColumn.Header>
</GridViewColumn>
<GridViewColumn Width="Auto" DisplayMemberBinding="{Binding Alarm}">
<GridViewColumn.Header>
<GridViewColumnHeader Content="{Binding Labels[2765]}" Tag="Alarm" Click="listViewStatEntriesColumnHeader_Click" Width="Auto" />
</GridViewColumn.Header>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
I found this thread: Force resize of GridView columns inside ListView and try Oskar solution:
public void AutoSizeColumns()
{
GridView gv = listView1.View as GridView;
if (gv != null)
{
foreach (var 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 (double.IsNaN(c.Width))
{
c.Width = c.ActualWidth;
}
c.Width = double.NaN;
}
}
}
But with this solution, columns was resized to visible contents only and not to all data into the ListView. How can I resize column to all data?
Thanks in advance for your help !
The reason they resize only to the visible rows is because you have virtualization on (which is default). Try turning it off
<ListView VirtualizingStackPanel.IsVirtualizing="False"
I'm trying to place a usercontrol in a flyout window. In this usercontrol I'm trying to bind a list to a listview, but the listview remains empty.
C# UserControl
public MusicList()
{
InitializeComponent();
}
public void Makemusiclist(Inhabitant inhabitant, Wait wait, Rectangle rect)
{
musicList.Width = rect.Width;
musicList.Height = rect.Height;
lViewMusicFiles.Width = rect.Width - 150; // place the box on the right plce on any screen
lViewMusicFiles.Height = rect.Height - 50;
List<MusicFile> playlist = new List<MusicFile>(); // get the inhabitants' playlist from his file
playlist = inhabitant.musicList;
lViewMusicFiles.DataContext = playlist;
lViewMusicFiles.ItemsSource = playlist;
lViewMusicFiles.Items.Refresh();
musicList.UpdateLayout();
Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() =>
{
wait.Close();
}));
}
XAML Usercontrol
<Grid>
<ListView x:Name="lViewMusicFiles" HorizontalAlignment="Left" VerticalAlignment="Top" Background="Red" Foreground="White" FontSize="32" Margin="150,50,0,0" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectionMode="Single" SelectionChanged="lViewMusicFiles_SelectionChanged" Style="{DynamicResource ListViewStyle1}" >
<ListView.View>
<GridView>
<GridViewColumn Header="Titel" Width="Auto" DisplayMemberBinding="{Binding Path=title}"/>
<GridViewColumn Header="Artist" Width="Auto" DisplayMemberBinding="{Binding Path=artist}"/>
<GridViewColumn Header="Album" Width="Auto" DisplayMemberBinding="{Binding Path=album}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</UserControl>
XAML flyout
<Grid>
<local:MusicList x:Name="musicList" />
</Grid>
Use ObservableCollection<T> instead of List<T>. you can write binding in XAML like this. I didn't write irrelevant properties.
<ListView x:Name="lViewMusicFiles" ItemsSource="{Binding Playlist, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}">
<ListView.View>
<GridView>
<GridViewColumn Header="Titel" DisplayMemberBinding="{Binding title}"/>
<GridViewColumn Header="Artist" DisplayMemberBinding="{Binding artist}"/>
<GridViewColumn Header="Album" DisplayMemberBinding="{Binding album}"/>
</GridView>
</ListView.View>
</ListView>
Also you need this property.
public ObservableCollection<MusicFile> Playlist{ get; } = new ObservableCollection<MusicFile>();
I'm Using WPF.
I have ListView with TextBox column and two checkboxs columns.
I want to edit the TextBox text by double click or something else.
What is the simple way to do that?
...
<GridViewColumn Header="Name" DisplayMemberBinding={Binding Path=fullName}" Width=500>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Name="txtName"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
...
Here is a sample way of doing this...
public partial class MainWindow : Window
{
public List<string> Items { get; set; }
public MainWindow()
{
InitializeComponent();
Items = new List<string>();
LoadItems();
DataContext = this;
}
private void txtName_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
TextBox currentTextBox = (TextBox)sender;
if (currentTextBox.IsReadOnly)
currentTextBox.IsReadOnly = false;
else
currentTextBox.IsReadOnly = true;
}
private void LoadItems()
{
Items.Add("Coffee");
Items.Add("Sugar");
Items.Add("Cream");
}
}
<Grid>
<ListView ItemsSource="{Binding Items}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Name="txtName" Text="{Binding Mode=OneTime}" IsReadOnly="True" MouseDoubleClick="txtName_MouseDoubleClick" Width="100"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
Here is an example I have from an application that I wrote. The column JobName was to be user editable. This example allows the column to be editable and also gets rid of the border and lets the background blend into the row so it doesn't appear to have a text box in it.
Those can be edited out (BorderThickness="0" Background="Transparent").
My example binds to an MVVM ViewModel property called JobName and is set to be "TwoWay" so that changes to the view model will also reflect on the UI.
<ListView x:Name="lvJobs" HorizontalAlignment="Left" Height="628" Margin="30,62,0,0" ItemsSource="{Binding Jobs}"
SelectedItem="{Binding SelectedJob, Mode=TwoWay}" VerticalAlignment="Top" Width="335">
<ListView.View>
<GridView>
<GridViewColumn Header="Active" Width="50">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsActive, Mode=TwoWay}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Job Name" Width="150">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding JobName, Mode=TwoWay}" BorderThickness="0" Background="Transparent"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding User}" Header="User" Width="125"/>
</GridView>
</ListView.View>
</ListView>
I am trying to add the values in the column Job but it keeps coming to 0. Why is this happening?
MainWindow.xaml.cs:
int jobSum = 0;
int bidSum = 0;
foreach (dynamic item in PlotListView.Items)
{
jobSum += Convert.ToInt32(item.Jobs);
bidSum += Convert.ToInt32(item.Bids);
}
JobBidRatioTextBlock.Text = jobSum.ToString();
MainWindow.xaml:
<ListView x:Name="PlotListView" HorizontalAlignment="Left" Height="228" Margin="5,37,0,0" VerticalAlignment="Top" Width="424">
<ListView.View>
<GridView AllowsColumnReorder="False">
<GridViewColumn Width="99" Header="Plot" DisplayMemberBinding="{Binding PlotId}"/>
<GridViewColumn Width="99" Header="Area" DisplayMemberBinding="{Binding Area}"/>
<GridViewColumn Width="99" Header="Jobs" DisplayMemberBinding="{Binding Jobs}"/>
<GridViewColumn Width="99" Header="Bids" DisplayMemberBinding="{Binding Bids}"/>
</GridView>
</ListView.View>
</ListView>
Details about background code running at: http://pastebin.com/z5kpDcUM
In you constructor for MainWindow just below InitializeComponent() write this:
InitializeComponent();
DependencyPropertyDescriptor
.FromProperty(ListView.ItemsSourceProperty, typeof(ListView))
.AddValueChanged(PlotListView, PlotListView_ItemsSourceChanged);
Then add a funtion to MainWindow Class:
private void PlotListView_ItemsSourceChanged(object sender, EventArgs e)
{
////Write your summation code here
}