开发者

wpf check list box

I m new to wpf.In order to get c开发者_JS百科heck list box functionality ,I have added below xaml to my code,but there is no output in my screen.only blank,what it could be?

<TabItem Header="Samples" >
            <ListBox Margin="10" Width="373" Height="236">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">                               
                            <TextBlock Text="MyText"/>
                            <CheckBox IsChecked="False"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </TabItem>


just have a look at this basic sample

http://merill.net/2009/10/wpf-checked-listbox/


List box is a bit wired for such task..Have a look at ItemsControl. Here is the code i use:

<ItemsControl            
    ItemsSource="{Binding ***}" IsTabStop="False">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <CheckBox
                Content="{Binding Name}"
                IsChecked="{Binding IsSelected}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>


Replace your code with this

<TabItem Header="Roles" >             
    <ListBox Margin="10" Width="373" Height="236">
        <ListBox.ItemTemplate>                     
            <DataTemplate>                         
               <StackPanel Orientation="Horizontal">
                   <TextBlock Text="MyText"/>
                   <CheckBox IsChecked="False"/>
               </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBoxItem>Hi</ListBoxItem>
    </ListBox>
</TabItem> 

and tell us if it still shows blank


Better still, just use the new CheckListBox control in the Extended WPF Toolkit http://wpftoolkit.codeplex.com/wikipage?title=CheckListBox&referringTitle=Home


This might help

1.Inorder to work datatemplate you must specify itemsource, here i have bounded a Stateslist a collection of items into it.

2.Also set the Datacontext to ViewModel or the CodeBehind as datacontext.

3.Datacontext will distribute the StateList properties collection to the listbox itemsource

using codebehind -

public Window1()
    {
        InitializeComponent();
        this.DataContext = this;
    LoadData();

    }

using viewmodel 

public Window1()
    {
        InitializeComponent();
        DataContext = new Window1ViewModel();
    LoadData();

    }



//MyItemsource Property for listbox

private ObservableCollection<States> _stateslist;
    public ObservableCollection<States> StatesList
    {
        get { return _stateslist; }
        set
        {
            _stateslist = value;
            RaisePropertyChanged(() => StatesList);
        }
    }

   // Sample Data Loading

public void LoadData()
    {
        StatesList = new ObservableCollection<States>();

        StatesList.Add(new States
        {
            StateName = "Kerala"
        });
        StatesList.Add(new States
        {
            StateName = "Karnataka"
        });
        StatesList.Add(new States
        {
            StateName = "Goa"
        });
    }

Window1.Xaml

  <ListBox  ItemsSource="{Binding StatesList}"  >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <CheckBox IsChecked="{Binding IsSelected"} Content="{Binding StateName}" />
                    <TextBox Text="{Binding TextBoxValue}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>


Check this out it isworking..you are using TabItem but you didnt define it in TabControl

  <TabControl>
  <TabItem Header="Tab1">
        <ListBox Margin="10" Width="373" Height="236">
            <ListBox.Items>

                    <StackPanel Orientation="Horizontal">                               
                        <TextBlock Text="MyText"/>
                        <CheckBox IsChecked="False"/>
                    </StackPanel>

            </ListBox.Items>
        </ListBox>
 </TabItem>
  </TabControl>

If you are new in WPF use XamlPadX it will give you great help to practice out on it..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜