I have a listview which I have binded (twoWay) to a datatable. The binding works fine and data is coming up properly on the listview. Now what I want to achieve is a bit complex and I am not even sure whether this can be achieved or now.
My datatable has say 15 columns, and I am showing 5 columns in the lisview. Is it possible that if the user selects a row on listview, I could display other 10 values for that selected row (from the datatable) in textblocks in stackpanel. Is this achievable or am I being too demanding? I tried to achieve this by getting ideas from question here, but couldn't achieve that. If it is achievable, can you guys give me ideas on how to proceed with this?
I think this might be achievable by handling listview1_selectionChanged event and populating the textboxes manually, but as I am in a learning stage, I wanted to explore if this can be done through databinding. This way I will know various ways of doing things and can build my concepts in the process.
I am attaching my code below. This is just a test project with one listview having one column.
XAML:
<Window.Resources>
<Prefs:Tables x:Key="TClass"></Prefs:Tables>
</Window.Resources>
<Grid>
<StackPanel Orientation="Horizontal">
<ListView Name="listView1" Background="Transparent" Height="534" BorderThickness="0 0 0 1" VerticalAlignment="Top">
<ListView.ItemsSource>
<Binding Source="{StaticResource TClass}" Path="Instance.dtAccounts" Mode="TwoWay"></Binding>
</ListView.ItemsSource>
<ListView.View>
<GridView x:Name="GridView1" ColumnHeaderContainerStyle="{StaticResource GridViewHeader}" AllowsColumnReorder="True">
<GridViewColumn Header="Company Name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="txbName" Padding="0 0 5 0" >
<TextBlock.Text>
<Binding Path="NAME">
</Binding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<StackPanel Name="stkPanel1" Margin="100 0 0 0">
<TextBlock></TextBlock>
</StackPanel>
</StackPanel>
</Grid>
Window1.xaml.cs
public partial class Window1 : Window
{
DataTable dt = new DataTable();
public Window1()
{
InitializeComponent();
Tables.Instance.dtAccounts = Worker.LoadAccounts();
}
}
Class Tables.cs
public class Tables : INotifyPropertyChanged
{
private static Tables instance;
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private DataTable _dtAccounts;
public Tables()
{
}
// Singleton instance read-only property
public static Tables Instance
{
get
{
if (instance == null)
{
instance = new Tables();
}
return instance;
}
}
public DataTable dtAccounts
{
get
{
return _dtAccounts;
}
set
{
_dtAccounts = value;
OnPropertyChanged("dtAccounts");
}
}
private void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
=====================
Final Working Code
I was able to do that with the help of answer provided by Phil. Posting my updated code below, as it might be useful for somebody else.
XAML:
<Grid>
<StackPanel Orientation="Horizontal">
<ListView Name="listView1" Background="Transparent" Height="534" BorderThickness="0 0 0 1" VerticalAlignment="Top" SelectionChanged="listView1_SelectionChanged">
<ListView.ItemsSource>
<Binding Source="{StaticResource TClass}" Path="Instance.dtAccounts" Mode="TwoWay"></Binding>
</ListView.ItemsSource>
<ListView.View>
<GridView x:Name="GridView1" ColumnHeaderContainerStyle="{StaticResource GridViewHeader}" AllowsColumnReorder="True">
<GridViewColumn Header="Company Name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Name="txbName" Padding="0 0 5 0" >
<TextBlock.Text>
<Binding Path="NAME">
</Binding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<StackPanel Name="stkPanel1" Margin="100 0 0 0">
<TextBlock>
<TextBlock.Text>
<Binding Source="{StaticResource TClass}" Path="Instance.SelectedName" Mode="TwoWay">
</Binding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</StackPanel>
</Grid>
Window1.xaml.cs
public partial class Window1 : Window
{
DataTable dt = new DataTable();
public Window1()
{
InitializeComponent();
Tables.Instance.dtAccounts = Worker.LoadAccounts();
}
private void listView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListView lstView = sender as ListView;
int item = lstView.SelectedIndex;
Tables.Instance.SetSelectedRow(item);
}
}
Tables.cs
public class Tables : INotifyPropertyChanged
{
private static Tables instance;
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private DataTable _dtAccounts;
private string _selectedName;
public Tables()
{
}
// Singleton instance read-only property
public static Tables Instance
{
get
{
if (instance == null)
{
instance = new Tables();
}
return instance;
}
}
public DataTable dtAccounts
{
get
{
return _dtAccounts;
}
set
{
_dtAccounts = value;
OnPropertyChanged("dtAccounts");
}
}
public string SelectedName
{
get
{
return _selectedName;
}
set
{
_selectedName = value;
OnPropertyChanged("SelectedName");
}
}
public void SetSelectedRow(int index)
{
int indexNo = index;
SelectedName = dtAccounts.Rows[index][0].ToString();
}
private void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}