Create item template for Combobox in code behind.

Answered Create item template for Combobox in code behind.

  • Tuesday, July 31, 2012 8:34 AM
     
     

    Hi all,

    I have working with this code.

    <ComboBox x:Name="cboTest" ItemSource="{Binding MyList, Mode=TwoWay}" SelectedValue={Binding Path=Name, Mode="TwoWay"}>
        <ComboBox.ItemTemplate>
        <DataTemplate>
        <StackPanel Orientation="Horizontal">
       <CheckBox Name="chkSelect'/>
        </StackPanel>
        </DataTemplate>
        </ComboBox.ItemTemplate>
        </ComboBox>

    to select multiple check box values inside the drop down.

    Now i need to do all these things from the code behind.

    Can any suggest how to define item template for combobox from the code behind in silverlight xaml.vb page.

    Thanks

    Shambhu

All Replies

  • Tuesday, July 31, 2012 5:13 PM
     
     Answered

    You can create a DataTemplate in code behind using XamlReader...  Once you have that, I assume you could assign it to your listbox.

    string template = "<DataTemplate xmlns=\"http://schemas.microsoft.com/client/2007\"><TextBlock Text=\"{Binding xx}x\" Foreground=\"Blue\" /></DataTemplate>";
    
    DataTemplate dt = XamlReader.Load(template) as DataTemplate;
    

  • Sunday, August 05, 2012 1:41 AM
     
     

    Hi Shambhu,

    Please try this:

    .xaml.cs

                DataTemplate template1 = (DataTemplate)XamlReader.Load(@"<DataTemplate xmlns=""http://schemas.microsoft.com/client/2007"">
                    <StackPanel Orientation=""Horizontal"">
                            <CheckBox Name=""chkSelect""/>
                            <TextBlock Text=""{Binding Name}""/>
                        </StackPanel>
                      </DataTemplate>");
                cboTest.ItemTemplate = template1;

    .xaml:

        <ComboBox x:Name="cboTest" Height="50" Width="150">
        </ComboBox>

    Best Regards,

  • Saturday, October 27, 2012 6:27 AM
     
     

    Thanks Dear,

    Let me apply this.

  • Saturday, October 27, 2012 6:44 AM
     
     

    It's saying

    Unexpected text after completed markup extension. [Line: 1 Position: 80]

  • Saturday, October 27, 2012 6:54 AM
     
     Answered

    thanks bro

    its my mistake, now working fine, thanks

    thank you very much.

  • Saturday, October 27, 2012 7:25 AM
     
     

    One minute sir,

    ok i have created the data template from code behind and is relfecting inside the combobox, but when i assigned Itemssource property to result list to combobox, text does not appearing, may be some binding with check box we have created through data template???

  • Saturday, October 27, 2012 10:09 AM
     
     

    Now i need to capture checked and unchecked event fo check box inside Item template in code beihind. Can you give me some idea, how to capture checked value on checked event.