开发者

Silverlight, how to make List of a UserControl (In ContentControl?)

i've been banging my head on this for the last hours...

I have a User Control called "DayItem", and i want to show it 48 times in another UserControl called "DayPanel". Let me mention this is done in MVVM style, but i'm only experiencing, and a straight way would by fine for an answer.

I have an ObservableCollection<DayItem> in the DayPanel model, and in the Xaml there's an <ItemsPresenter />.

if i do

this.ItemsSource = DayItems;

everything show up fine. but, i wanna be able to use those DayItems in the UI like a list... to support multi-select etc开发者_StackOverflow.

so i tried using a ContentControl, and set it's content to the ObservableCollection. but it just shows the ObservableCollection object's ToString text. so i guess i need a DataTemplete there... but why do i need a DataTemple to show a Control? it's already styled in it's own Xaml, i don't wanna repeat it's styling again.

or maybe i'm totally wrong, anyway i need help :x

Edit:

I got this to work, saying what DataType wasn't necessary or even possible. and in the code behind i told the listbox, that it's ItemSource was the ObservableCollection.

now i've ran into other problems... ListBox related... There are Gaps between each control in the ListBox, which messes up the layout and also i need to figure out a way to select multiple items by dragging...

thanks for the help so fat


First, you need a view model for you DayItem user control. Lets call it DayItemViewModel. Also I suppose you DayPanel also has a view model called something like DayPanelViewModel. Then, you DayPanelViewModel would expose a collection of DayItemViewModel instances:

public class DayPanelViewModel
{
    public ObservableCollection<DayItemViewModel> DayItems { get; set; }
}

Then, in your DayPanel.xaml:

<UserControl x:Class="DayPanel"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <UserControl.Resources>
        <DataTemplate x:Key="DayItemTemplate"
                      DataType="{x:Type my:DayItemViewModel}">
            <my:DayItem />
        </DataTemplate>
    </UserControl.Resources>
    <Grid>
        <ListBox ItemsSource="{Binding DayItems}" 
                 ItemTemplate="{StaticResource DayItemTemplate}" />
    </Grid>
</UserControl>


Try using ListBox, since that implements multiselect... Also it might be wise (for MVVM) if you do not contain DayItems, but DayItemModel's in your DayPanelModel, and set the ListBox's ItemTemplate to present each DayItemModel with a DayItem.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜