How can I bind the listview Item Properties of a selected item?
At the left side I show my files with sites in the listview and at the right i want to show the name, number of sites etc in a kind of Infobox (Grid with labels).
So how can I bind the list[index].name to the name label when i select it in the listview?
Or should I work with Selection Changed Event?
You Could bind the SelectedItem property of the GridView to your property on the ViewModel:
<DataGrid SelectedItem="{Binding SelectedItem, Mode=TwoWay}" >
.....
</DataGrid>
and in SelectedItem property:
private YourItemType_selectedItem;
public YourItemType SelectedItem
{
get { return _selectedItem; }
set
{
if(value != _selectedItem)
{
_selectedItem = value;
NotifyOfPropertyChange("SelectedItem");
// notify InfoBox property changed
NotifyOfPropertyChange("InfoBoxItem");
}
}
}
You could bind the TextBlocks/TextBoxes in the InfoBox to the SelectedItem property of the ListView.
Give the ListView an x:Name:
<ListView x:Name="listView">
<ListView.View>
<GridView>
<GridViewColumn Width="160" DisplayMemberBinding="{Binding Pos}" Header="Pos." />
<GridViewColumn Width="160" DisplayMemberBinding="{Binding Name}" Header="Name" />
<GridViewColumn Width="160" DisplayMemberBinding="{Binding Seitenzahl}" Header="Seitenzahl" />
</GridView>
</ListView.View>
</ListView>
...and use use an ElementName binding:
<TextBlock Text="{Binding SelectedItem.Name, ElementName=listView}" />
<TextBlock Text="{Binding SelectedItem.Pos, ElementName=listView}" />
<TextBlock Text="{Binding SelectedItem.Seitenzahl, ElementName=listView}" />
"Name", "Pos" and "Seitenzahl" are the name of the same properties that you bind to and display in the columns of the ListView.
Related
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>
How can I get the selected item from clicking a context menu bound to a listview control in WPF?
This is my markup:
<ListView Name="lvCustomerJobs">
<ListView.ContextMenu>
<ContextMenu>
<MenuItem Header="Remove"
Click="cmCustomerRemoveJob"
Command="{Binding RemoveItem}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu},
Path=PlacementTarget.SelectedItem}" />
</ContextMenu>
</ListView.ContextMenu>
<ListView.View>
<GridView>
<GridViewColumn Header="Status" Width="150" DisplayMemberBinding="{Binding Status}" />
<GridViewColumn Header="Booked in by" Width="150" DisplayMemberBinding="{Binding BookedInBy}" />
<GridViewColumn Header="Date Required" Width="150" DisplayMemberBinding="{Binding DateRequired}" />
</GridView>
</ListView.View>
</ListView>
This is my code behind:
private void cmCustomerRemoveJob(object sender, RoutedEventArgs e)
{
var item = ((FrameworkElement)e.OriginalSource).DataContext as User;
if (item != null)
{
MessageBox.Show(item.DateRequired + " Item's Double Click handled!");
}
}
But item IS null?
You should cast sender object to MenuItem and then use CommandParameter like this:
private void cmCustomerRemoveJob(object sender, RoutedEventArgs e)
{
var item = ((MenuItem)sender).CommandParameter as User;
if (item != null)
{
MessageBox.Show(item.DateRequired + " Item's Double Click handled!");
}
}
I bind my ListView to a collection in the model and bind the SelectedItem to the model as well.
<ListView ItemSource="{Binding CustomerCollection}" SelectedItem="{Binding SelectedCustomer}">
Then, my command method can reference SelectedCustomer as needed.
If this isn't ideal for some reason, I'd love to know!
I have a 2 column ListView and I'm trying to fill it using and IDictionary with the following code.
System.Collections.IDictionary entryList = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User);
foreach (System.Collections.DictionaryEntry de in entryList)
{
Row row = new Row();
row.Name1 = (string)de.Key;
row.Name2 = (string)de.Value;
this.list1.Items.Add(row);
}
public class Row
{
public string Name1 { get; set; }
public string Name2 { get; set; }
}
XAML:
<ListView x:Name="varList"
Grid.ColumnSpan="1"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Height="400"
Width="500"
Margin="0, 30, 0, 0">
<ListView.View>
<GridView>
<GridViewColumn Width="150" Header="Name" />
<GridViewColumn Width="350" Header="Path" />
</GridView>
</ListView.View>
</ListView>
But every row and column gets filled with "Project.Views.Row".
Anyone got any idea on how to fix it? Thank you very much.
A ListView (and every other control for that matter) will display the results of calling ToString when given an object to display.
For a standard class, thats its qualified name; Project.Views.Row in your example.
There are two ways to fix this:
Don't add an object. Instead, format the string as you want it ie:
list1.Items.Add(String.Format({0}:{1}, row.Name1, row.Name2));
Do this the right way and use MVVM. In this case your XAML needs a data template:
<ListView ItemsSource="{Binding Rows}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name1}"/>
<TextBlock Text="{Binding Name2}"/>
</StackPanel>
</DataTemplate>
<ListView.ItemTemplate>
</ListView>
For a grid view:
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name1}" />
<GridViewColumn Header="Path" DisplayMemberBinding="{Binding Name2}"/>
</GridView>
</ListView.View>
Binding the ItemsSource is not actually necessary, but since we are doing everything the right way, you should do it so you are not directly manipulating the UI from code.
I have this setup for my ListView and I am trying to make Binding to work in my MVVM structure.
This is the xaml code:
<Window.Resources>
<CollectionViewSource
x:Key="DeviceList"
Source="{Binding Path=DiscoveredDevicesList}">
</CollectionViewSource>
</Window.Resources>
<ListView
Grid.Row="1"
Width="500"
HorizontalAlignment="Left"
Margin="10"
Grid.Column="1"
DataContext="{StaticResource DeviceList}"
ItemsSource="{Binding}">
<ListView.View>
<GridView>
<GridViewColumn Header="Device name" DisplayMemberBinding="{Binding Path=DeviceName}"/>
<GridViewColumn Header="Rssi" DisplayMemberBinding="{Binding Path=Rssi}"/>
<GridViewColumn Header="GPS Row" DisplayMemberBinding="{Binding Path=GpsSignal}"/>
</GridView>
</ListView.View>
</ListView>
And the following is the code to implement the binding
//Constructor
public MainWindowViewModel()
{
DiscoveredDevicesList = new ObservableCollection<MyDeviceInfo>();
}
private ObservableCollection<MyDeviceInfo> _DiscoveredDevicesList;
public ObservableCollection<MyDeviceInfo> DiscoveredDevicesList
{
get
{
return _DiscoveredDevicesList;
}
set
{
_DiscoveredDevicesList = value;
OnPropertyChanged("DiscoveredDevicesList");
}
}
Using Add() and Clear() updates the view just fine when used as below:
var client = new BluetoothClient();
DiscoveredDevicesList.Clear();
IEnumerable<MyDeviceInfo> csaDevices = null;
csaDevices = await DiscoverCsaDevicesAsync(client);
foreach (var csaDevice in csaDevices)
{
DiscoveredDevicesList.Add(csaDevice);
DiscoveredDevicesList.First().GpsSignal = true;
}
So I can see the value of GpsSignal as changed to ture from initial opposite in my view.
However, if I put the following same line in the OnClick of a button doesn't do the same and the value stays false since I am not using any Add() or Clear() and I simply rely on the OnPropertyChanged to do the trick for me.
DiscoveredDevicesList.First().GpsSignal = true;
The rest of the bindings such as button clicks and textblock information work fine, then I would assume it shouldn't be the problem with implementation of the binding on the back of the stage.
Would appreciate your suggestions on this.
if your MyDeviceInfo class implement INotifyPropertyChanged and your property GpsSignal raise OnPropertyChanged() you should see all changes when your bindings are right.
nevertheless i would change your code in this way:
remove CollectionViewSource from your resources and use simple binding
<Window.Resources>
</Window.Resources>
<ListView ItemsSource="{Binding DeviceList}">
<ListView.View>
<GridView>
<GridViewColumn Header="Device name" DisplayMemberBinding="{Binding Path=DeviceName}"/>
<GridViewColumn Header="Rssi" DisplayMemberBinding="{Binding Path=Rssi}"/>
<GridViewColumn Header="GPS Row" DisplayMemberBinding="{Binding Path=GpsSignal}"/>
</GridView>
</ListView.View>
</ListView>
set the datacontext for your window to your MainWindowViewModel in codebehind or xaml.
instead of using OnClick, pls use ICommands (RelayCommand or DelegateCommand)
<Button Command="{Binding MyCommand4GpsSignalIsTrue}" />
Hi i'm working in windows store app with MVVM pattern and i have some problem in catch the listview itemclick value in relay command. Now i got the selected item value.But don't know how to get itemclickValue. Here i have attach my code.
XAML
<ListView x:Name="lstItem" ItemTemplate="{StaticResource ItemTemplate}" ItemsSource="{Binding ItemList}" Padding="130,0,0,0" SelectedItem="{Binding SelectedItem,Mode=TwoWay}">
<Triggers:Interactions.Triggers>
<Triggers:EventTrigger EventName="SelectionChanged">
<Triggers:InvokeCommandAction Command="{Binding SelectedItemCommand}" CommandParameter="{Binding SelectedItem,Mode=TwoWay}"/>
</Triggers:EventTrigger>
</Triggers:Interactions.Triggers>
</ListView>
ViewModel Code
private Item _selectedItem;
public Item SelectedItem { get { return _selectedItem; } set { _selectedItem = value; NotifyPropertyChanged("SelectedTrends"); } }
private RelayCommand<Item> _selectedItemCommand;
public RelayCommand<Item> SelectedItemCommand
{
get
{
return this._selectedItemCommand
?? (this._selectedItemCommand= new RelayCommand<Item>(item=>
{
MessageDialog messagedialog = new MessageDialog(item.Name,"Test");
messagedialog.ShowAsync();
}));
}
}
There's a bit of redundancy here:
Option 1: Spare the CommandParameter:
private Item _selectedItem;
public Item SelectedItem
{
get { return _selectedItem; }
set { _selectedItem = value; NotifyPropertyChanged("SelectedTrends"); }
}
private RelayCommand _selectedItemCommand;
public RelayCommand SelectedItemCommand
{
get
{
return this._selectedItemCommand
?? (this._selectedItemCommand= new RelayCommand(() =>
{
MessageDialog messagedialog = new MessageDialog(selectedItem.Name,"Test");
messagedialog.ShowAsync();
}));
}
}
and the XAML:
<ListView x:Name="lstItem" ItemTemplate="{StaticResource ItemTemplate}" ItemsSource="{Binding ItemList}" SelectedItem="{Binding SelectedItem,Mode=TwoWay}" Padding="130,0,0,0">
<Triggers:Interactions.Triggers>
<Triggers:EventTrigger EventName="SelectionChanged">
<Triggers:InvokeCommandAction Command="{Binding SelectedItemCommand}" />
</Triggers:EventTrigger>
</Triggers:Interactions.Triggers>
</ListView>
Option 2: Spare the SelectedItem binding:
<ListView x:Name="lstItem" ItemTemplate="{StaticResource ItemTemplate}" ItemsSource="{Binding ItemList}" Padding="130,0,0,0">
<Triggers:Interactions.Triggers>
<Triggers:EventTrigger EventName="SelectionChanged">
<Triggers:InvokeCommandAction Command="{Binding SelectedItemCommand}" CommandParameter="{Binding SelectedItem, ElementName=lstItem}"/>
</Triggers:EventTrigger>
</Triggers:Interactions.Triggers>
</ListView>
and remove the SelectedItem property from the ViewModel, unless you need it for something else.
EDIT
If you want to handle the click event on an item, you need to move the behavior to the ItemTemplate DataTemplate parent control, for example the grid in which the controls are placed. This lets you handle the click event on the item.
To resolve the problem I evaluated the setter attributed if there is Null reference. Then it worked fine and the event wasn't thrown anymore choose other elements.
<ListView Name="lstView" ItemsSource="{Binding Path=SimResults}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding SelectedItemCommand}" CommandParameter="{Binding SelectedItem, ElementName=lstView}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="FileUniqueID" Width="Auto" DisplayMemberBinding="{Binding Path=FileUniqueID}" />
<GridViewColumn Header="XML" Width="Auto" DisplayMemberBinding="{Binding Path=XML}" />
<GridViewColumn Header="Request" Width="Auto" HeaderStringFormat="" DisplayMemberBinding="{Binding Path=RequestShort}" />
<GridViewColumn Header="RequestDate" Width="Auto" DisplayMemberBinding="{Binding Path=RequestDate}" />
<GridViewColumn Header="Response" Width="Auto" DisplayMemberBinding="{Binding Path=ResponseShort}" />
<GridViewColumn Header="ResponseDate" Width="Auto" DisplayMemberBinding="{Binding Path=ResponseDate}" />
<GridViewColumn Header="ResendCounter" Width="Auto" DisplayMemberBinding="{Binding Path=ResendCounter}" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>