ठीक है, यह मुझे थोड़ी देर के लिए परेशान कर रहा है। और मुझे आश्चर्य है कि कैसे दूसरों को निम्नलिखित मामला संभाल:कॉम्बोबॉक्स आइटमसोर्स बदल गया => चयनित इटिम बर्बाद हो गया है
<ComboBox ItemsSource="{Binding MyItems}" SelectedItem="{Binding SelectedItem}"/>
DataContext ऑब्जेक्ट के कोड:
public ObservableCollection<MyItem> MyItems { get; set; }
public MyItem SelectedItem { get; set; }
public void RefreshMyItems()
{
MyItems.Clear();
foreach(var myItem in LoadItems()) MyItems.Add(myItem);
}
public class MyItem
{
public int Id { get; set; }
public override bool Equals(object obj)
{
return this.Id == ((MyItem)obj).Id;
}
}
जाहिर है जब RefreshMyItems()
विधि कॉम्बो बॉक्स कहा जाता है संग्रह बदली गई घटनाओं प्राप्त करता है, अपने आइटम अद्यतन करता है और करता है ताज़ा संग्रह में चयनित इटैम नहीं ढूंढें => चयनित इटैम को null
पर सेट करता है। लेकिन नए संग्रह में सही आइटम का चयन करने के लिए मुझे Equals
विधि का उपयोग करने के लिए कॉम्बो बॉक्स की आवश्यकता होगी।
दूसरे शब्दों में - आइटम्ससोर्स संग्रह में अभी भी सही MyItem
है, लेकिन यह new
ऑब्जेक्ट है। और मैं कॉम्बो बॉक्स को Equals
जैसे कुछ स्वचालित रूप से चुनने के लिए उपयोग करना चाहता हूं (यह भी कठिन होता है क्योंकि पहले स्रोत संग्रह Clear()
पर कॉल करता है जो संग्रह को रीसेट करता है और पहले से ही चयनित इटैम null
पर सेट होता है)।
अद्यतन 2 नीचे दिए गए कोड को कॉपी करने से पहले कृपया ध्यान दें कि यह पूर्णता से बहुत दूर है! और ध्यान दें कि यह डिफ़ॉल्ट रूप से दो तरीकों से बंधे नहीं है।
public static class CBSelectedItem
{
public static object GetSelectedItem(DependencyObject obj)
{
return (object)obj.GetValue(SelectedItemProperty);
}
public static void SetSelectedItem(DependencyObject obj, object value)
{
obj.SetValue(SelectedItemProperty, value);
}
// Using a DependencyProperty as the backing store for SelectedIte. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectedItemProperty =
DependencyProperty.RegisterAttached("SelectedItem", typeof(object), typeof(CBSelectedItem), new UIPropertyMetadata(null, SelectedItemChanged));
private static List<WeakReference> ComboBoxes = new List<WeakReference>();
private static void SelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ComboBox cb = (ComboBox) d;
// Set the selected item of the ComboBox since the value changed
if (cb.SelectedItem != e.NewValue) cb.SelectedItem = e.NewValue;
// If we already handled this ComboBox - return
if(ComboBoxes.SingleOrDefault(o => o.Target == cb) != null) return;
// Check if the ItemsSource supports notifications
if(cb.ItemsSource is INotifyCollectionChanged)
{
// Add ComboBox to the list of handled combo boxes so we do not handle it again in the future
ComboBoxes.Add(new WeakReference(cb));
// When the ItemsSource collection changes we set the SelectedItem to correct value (using Equals)
((INotifyCollectionChanged) cb.ItemsSource).CollectionChanged +=
delegate(object sender, NotifyCollectionChangedEventArgs e2)
{
var collection = (IEnumerable<object>) sender;
cb.SelectedItem = collection.SingleOrDefault(o => o.Equals(GetSelectedItem(cb)));
};
// If the user has selected some new value in the combo box - update the attached property too
cb.SelectionChanged += delegate(object sender, SelectionChangedEventArgs e3)
{
// We only want to handle cases that actually change the selection
if(e3.AddedItems.Count == 1)
{
SetSelectedItem((DependencyObject)sender, e3.AddedItems[0]);
}
};
}
}
}
iv'e इस मुद्दे में आए और निम्नलिखित तरीके से इसे हल http://stackoverflow.com/questions/12337442/proper-use-of-propertychangedtrigger-and-changepropertyaction/12341649#12341649 –