Start a new WPF Application. We'll be working in C# today.
Let's set up a XamDataGrid to display a few rows and bind a TextBlock to the selected item.
<Window x:Class="SelectedDataItem.MainWindow"
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:SelectedDataItem"
xmlns:igDP="http://infragistics.com/DataPresenter"
mc:Ignorable="d"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="350"
Width="525">
<StackPanel Orientation="Vertical">
<igDP:XamDataGrid Height="250" DataSource="{Binding Items}" SelectedDataItem="{Binding SelectedItem}">
</igDP:XamDataGrid>
<TextBlock Text="{Binding SelectedItem.ItemName}"/>
</StackPanel>
</Window>
-------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
namespace SelectedDataItem
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
public class cItem
{
public String ItemName { get; set; }
}
public List<cItem> Items { get; set; }
private cItem _SelectedItem;
public cItem SelectedItem
{
get
{ return _SelectedItem; }
set
{ _SelectedItem = value;
NotifyPropertyChanged("SelectedItem");
}
}
public MainWindow()
{
Items = new List<cItem>();
Items.Add(new cItem() { ItemName = "This is item 1" });
Items.Add(new cItem() { ItemName = "This is item 2" });
Items.Add(new cItem() { ItemName = "This is item 3" });
InitializeComponent();
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if
(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
}
The problem is that even though it looks as though the row is being selected, it isn't. Try setting a breakpoint on the SelectedItem's setter - it's never called.
Add the following XAML in the XamDataGrid. It tells the grid to select the record when the user clicks in a cell. I don't know why this isn't the default.
<igDP:XamDataGrid.FieldSettings>
<igDP:FieldSettings CellClickAction="SelectRecord"/>
</igDP:XamDataGrid.FieldSettings>
Now try running the program again and selecting a record.
The answer's always simpler when you know it.
No comments:
Post a Comment