निम्नलिखित उदाहरण चलाएं और आप देखेंगे कि यह क्यों काम नहीं करता है।
XAML:
<Window x:Class="DataGridTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<DockPanel>
<TextBlock DockPanel.Dock="Bottom" Text="{Binding SelectedItem, ElementName=dataGrid}"/>
<TextBlock DockPanel.Dock="Bottom" Text="{Binding SelectedItem}"/>
<DataGrid x:Name="dataGrid" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" CanUserAddRows="True" CanUserDeleteRows="True" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"/>
<DataGridTextColumn Header="Last Name" Binding="{Binding FirstName}"/>
</DataGrid.Columns>
</DataGrid>
</DockPanel>
</Window>
कोड-पीछे:
namespace DataGridTest
{
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
public partial class MainWindow : Window, INotifyPropertyChanged
{
private readonly ICollection<Person> items;
private Person selectedItem;
public MainWindow()
{
InitializeComponent();
this.items = new ObservableCollection<Person>();
this.items.Add(new Person
{
FirstName = "Kent",
LastName = "Boogaart"
});
this.items.Add(new Person
{
FirstName = "Tempany",
LastName = "Boogaart"
});
this.DataContext = this;
}
public ICollection<Person> Items
{
get { return this.items; }
}
public Person SelectedItem
{
get { return this.selectedItem; }
set
{
this.selectedItem = value;
this.OnPropertyChanged("SelectedItem");
}
}
private void OnPropertyChanged(string propertyName)
{
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class Person
{
public string FirstName
{
get;
set;
}
public string LastName
{
get;
set;
}
public override string ToString()
{
return FirstName + " " + LastName;
}
}
}
आप, जब चल रहा है 'नई' पंक्ति को चुनकर देख सकते हैं एक प्रहरी मूल्य का कारण बनता है में चयनित आइटम के रूप में स्थापित किया जाना DataGrid
। हालांकि, डब्ल्यूपीएफ उस सेंटीनेल आइटम को Person
में परिवर्तित करने में असमर्थ है, इसलिए SelectedItem
बाध्यकारी रूपांतरित करने में विफल रहता है।
इसे ठीक करने के लिए, आप अपने बाध्यकारी पर कनवर्टर डाल सकते हैं जो सेंटीनेल का पता लगाता है और पता चला जब null
देता है। यहाँ एक कनवर्टर है कि ऐसा नहीं करता है है:
namespace DataGridTest
{
using System;
using System.Windows.Data;
public sealed class SentinelConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null && string.Equals("{NewItemPlaceholder}", value.ToString(), StringComparison.Ordinal))
{
return null;
}
return value;
}
}
}
आप देख सकते हैं, यह प्रहरी की ToString()
मूल्य के खिलाफ परीक्षण करने के लिए एक दुर्भाग्यपूर्ण आवश्यकता है, क्योंकि यह एक आंतरिक प्रकार है। आप वैकल्पिक रूप से (या इसके अतिरिक्त) जांच सकते हैं कि GetType().Name
NamedObject
है।
स्रोत
2012-02-02 09:03:08
5 साल बाद, लेकिन इसे यहां भी रखा जा सकता है: आप इसे अपने संग्रह पर "{NewItemPlaceholder}" स्ट्रिंग को जलाने की आवश्यकता के बिना, संग्रह View.NewItemPlaceholder के साथ तुलना कर सकते हैं, जो आप कर रहे हैं। –