Reputation: 117
I'm very new to C# and WPF. I've started new project to learn how to use them both together and how to build UI. Bearly started i've stuck on using selecteditem from listview collection. I was trying to display some basic info taken from object selected. On the form I've aded label and some buttons. Main goal was to open second form with detailed info about selected record. But first I wanted to achieve something simple - just to display record ID in label controll. I can get records and fill the listbox with records, but all atempts to read selected data failed (nothing shows in label). Could you please help me and show me how to use selecteditem with label scenario? And hopefully give me some advices on the details window scenario too... Anyway - all comments regarding my code will be appreciated, plese be patient and remember that this is my first approach to this subject.
For your convenience all the code is available at BitBucket: https://bitbucket.org/is-smok/gama
Thank you for any help.
Part of the MainWindow.xaml file
<Grid>
<ListView x:Name="lstInventory" Height="180" Margin="5,51,79,0" VerticalAlignment="Top"
ItemsSource="{Binding GetInventory}"
SelectedItem="{Binding SelectedInventory, Mode=TwoWay}"
DisplayMemberPath="Inventory_id">
<ListView.View>
<GridView>
<GridViewColumn Header="GamaID" DisplayMemberBinding="{Binding Inventory_id}" />
<GridViewColumn Header="Typ" DisplayMemberBinding="{Binding Serial_number}" />
<GridViewColumn Header="Producent" DisplayMemberBinding="{Binding Registry_number}" />
</GridView>
</ListView.View>
</ListView>
<Button Content="Add" HorizontalAlignment="Left" Margin="57,265,0,0" VerticalAlignment="Top" Width="75" Click="AddInventory_Click"/>
<Button Content="Remove" HorizontalAlignment="Left" Margin="137,265,0,0" VerticalAlignment="Top" Width="75" Click="RemoveInventory_Click"/>
<Button Content="Edit" HorizontalAlignment="Left" Margin="217,265,0,0" VerticalAlignment="Top" Width="75" Click="EditInventory_Click"/>
<Label Content="{Binding SelectedInventory.Serial_number}" HorizontalAlignment="Left" Margin="120,326,0,0" VerticalAlignment="Top" Height="24" Width="140"/>
<Label x:Name="lblInventoryId" Content="{Binding SelectedInventory.Inventory_id}" HorizontalAlignment="Left" Margin="10,326,0,0" VerticalAlignment="Top" Height="24" Width="105"/>
</Grid>
Part of MainWindow.xaml.cs
public MainWindow()
{
InitializeComponent();
DataAccess dataAccess = new DataAccess();
inventory = dataAccess.GetIventory();
lstInventory.ItemsSource = inventory;
lstInventory.DisplayMemberPath = "inventory_id";
}
Part of DataAccess.cs file
private Inventory m_SelectedInventory;
public Inventory SelectedInventory
{
get
{
return m_SelectedInventory;
}
set
{
m_SelectedInventory = value;
}
}
Upvotes: 1
Views: 317
Reputation: 169420
Could you please help me and show me how to use selecteditem with label scenario?
You could bind directly to the SelectedItem
property of the ListView
:
<Label x:Name="lblInventoryId" Content="{Binding SelectedItem.Inventory_id, ElementName=lstInventory}" ... />
But what you should do to be able to bind to your SelectedInventory
property is to set the DataContext
of the window to the DataAccess
object:
public MainWindow()
{
InitializeComponent();
DataContext = new DataAccess();
}
Then the bindings should work provided that GetInventory
that you are binding to in your XAML is a public property of the DataAccess
class:
ItemsSource="{Binding GetInventory}"
You can't bind to a method. So you should call the GetIventory()
method in your DataAccess
class and exopose the results through a property, e.g.:
public DataAccess
{
public DataAccess()
{
Inventories = GetIventory();
}
public IEnumerable Inventories { get; private set; }
//...
}
XAML:
ItemsSource="{Binding Inventories}"
Also note that DataAccess
should implement the INotifyPropertyChanged
interface and raise a notification to the UI every time the SelectedInventory
property is set. Please refer to MSDN for more information about this.
Upvotes: 2
Reputation: 279
Hi one of many problems is that the Label gets the value only at initialization. The SelectedInventory
is updated correctly but the Label does not recognize it. To achieve this read about the INotifyPropertyChanged
Interface, for more information here
Implementation of the Interface:
public event PropertyChangedEventHandler PropertyChanged;
public Inventory SelectedInventory
{
get
{
return m_SelectedInventory;
}
set
{
m_SelectedInventory = value;
RaisePropertyChanged(nameof(SelectedInventory));
}
}
private void RaisePropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
Upvotes: 1
Reputation: 1253
ListView's ItemsSource has to be binded to property of type IEnumerable or derived, binding to method is not supported in wpf.
Upvotes: 0