मेरे पास एक WPF ListBox
नियंत्रण है और मैं आइटम ऑब्जेक्ट्स के संग्रह में ItemsSource
सेट कर रहा हूं। ListBoxItem
की Selected
के रूप में सेट करने के लिए ऑब्जेक्ट का उदाहरण किए बिना Selected
संबंधित आइटम ऑब्जेक्ट की संपत्ति को IsSelected
संपत्ति कैसे बांध सकता है?ListBoxItem की IsSelected प्रॉपर्टी को इसके स्रोत से ऑब्जेक्ट पर किसी प्रॉपर्टी पर बाध्यकारी
17
A
उत्तर
34
बस ItemContainerStyle ओवरराइड:
<ListBox ItemsSource="...">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="IsSelected" Value="{Binding Selected}"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
ओह, वैसे, मुझे लगता है कि आप dr.WPF से इस अद्भुत लेख चाहते हैं: ItemsControl: A to Z।
उम्मीद है कि इससे मदद मिलती है।
2
मैं कोड में समाधान ढूंढ रहा था, इसलिए इसका अनुवाद यहां है।
System.Windows.Controls.ListBox innerListBox = new System.Windows.Controls.ListBox();
//The source is a collection of my item objects.
innerListBox.ItemsSource = this.Manager.ItemManagers;
//Create a binding that we will add to a setter
System.Windows.Data.Binding binding = new System.Windows.Data.Binding();
//The path to the property on your object
binding.Path = new System.Windows.PropertyPath("Selected");
//I was in need of two way binding
binding.Mode = System.Windows.Data.BindingMode.TwoWay;
//Create a setter that we will add to a style
System.Windows.Setter setter = new System.Windows.Setter();
//The IsSelected DP is the property of interest on the ListBoxItem
setter.Property = System.Windows.Controls.ListBoxItem.IsSelectedProperty;
setter.Value = binding;
//Create a style
System.Windows.Style style = new System.Windows.Style();
style.TargetType = typeof(System.Windows.Controls.ListBoxItem);
style.Setters.Add(setter);
//Overwrite the current ItemContainerStyle of the ListBox with the new style
innerListBox.ItemContainerStyle = style;
+3
हैलो ब्रैंडन, शायद दोनों समाधान अच्छी तरह से काम करते हैं, हालांकि जब संभव हो तो कृपया यूआई व्यवहार को परिभाषित करने के लिए एक्सएमएल घोषणात्मक दृष्टिकोण पसंद करें। इस तरह अधिक लोग (इंटरैक्शन देव, आदि) इसे समझ सकते हैं और इसे आसानी से संशोधित कर सकते हैं। सम्मान, – wacdany
यह वही है जो मैं ढूंढ रहा था। धन्यवाद। – BrandonS
अफसोस की बात यह है कि यह WinRT के साथ काम नहीं करता है क्योंकि [बाइंडिंग्स सेटर्स पर समर्थित नहीं हैं] (http://stackoverflow.com/a/11869065/641833)। – Trisped