Thursday, 15 March 2012

binding - WPF Combobox, bind a collection where item has a bool property isSelected? -



binding - WPF Combobox, bind a collection<items> where item has a bool property isSelected? -

i have list< versions> version, amongst others has properties versionuuid, label, sku , isselected. bind combobox , have selected item select isselected flag (unselected previous set flag).

note:the combobox in template, used within datagrid cell, can not bind selecteditem model!

what have far working, datagrid updates db expected, initial value not set onload. if 1 version has isselected=true, have showing int combobox, empty unless select 1 list.

<datatemplate x:key="dtdatagridversionselector"> <combobox margin="0" width="90" style="{staticresource datagridcombobox}" itemssource="{binding path=versions, mode=onetime}"> <combobox.itemtemplate > <datatemplate > <radiobutton focusable="false" isenabled="true" groupname="{binding versionuuid}" ischecked="{binding isselected, mode=twoway}"> <stackpanel orientation="horizontal" > <textblock margin="3,0,0,0" text="{binding label}"/> <textblock foreground="red" margin="3,0,0,0" text="{binding sku}"/> </stackpanel> </radiobutton> </datatemplate> </combobox.itemtemplate> <combobox.itemcontainerstyle> <style targettype="comboboxitem"> <setter property="isselected" value="{binding isselected, mode=oneway}" /> </style> </combobox.itemcontainerstyle> </combobox> </datatemplate>

also, utilize of radiobox not written in stone, if there improve solution accomplish 1 item isselected, i'm open it

thanx pointers andreas

<window x:class="wpfapplication1.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:wpfapplication1" title="mainwindow" height="350" width="525"> <window.resources> <local:selecteditemconverter x:key="selecteditemconverter"/> </window.resources> <grid> <combobox itemssource="{binding students}" selecteditem="{binding students, converter={staticresource selecteditemconverter}}" displaymemberpath="name"/> </grid>

public partial class mainwindow : window { public mainwindow() { initializecomponent(); students = new observablecollection<student>(); students.add(new student() { name = "harish", rollno = 1, isselected = false }); students.add(new student() { name = "arev", rollno = 2, isselected = false }); students.add(new student() { name = "pankaj", rollno = 3, isselected = true }); students.add(new student() { name = "deepak", rollno = 4, isselected = false }); datacontext = this; } public observablecollection<student> students { get; set; } } public class pupil { public string name { get; set; } public int rollno { get; set; } public bool isselected { get; set; } } public class selecteditemconverter : ivalueconverter { #region ivalueconverter members public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { if (value != null && value ienumerable<student>) homecoming ((ienumerable<student>)value).where(s => s.isselected).firstordefault(); homecoming null; } public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { throw new notimplementedexception(); } #endregion }

i hope help.

wpf binding combobox selecteditem

No comments:

Post a Comment