Daniel Bişar
Daniel Bişar

Reputation: 2773

WPF Dashboard and Widgets

Hi I would like to write some kind of dashboard. You should be able to drag widgets on the dashboard from some source. The layout of the widgets should be free (first Canvas, later some own Panel).

My questions:

Upvotes: 0

Views: 1915

Answers (1)

Rachel
Rachel

Reputation: 132558

I would make a BaseClass for all widgets, and then build a ViewModel that inherits from that BaseClass for each Widget, along with a View to go with that ViewModel

After that, I would have something like ObservableCollection<WidgetBaseViewModel> OpenWidgets in the main application ViewModel, and bind it to an ItemsControl.

The ItemsControl would have it's ItemsPanelTemplate set to a Canvas, and each WidgetBaseViewModel would contain a Top, Left, Height, and Width property.

The actual UI to display each Widget with would be based on a DataTemplate, and could be anything you want, although a UserControl would be easiest

<ItemsControl ItemsSource="{Binding OpenWidgets}">
    <ItemsControl.Resources>
        <DataTemplate DataType="{x:Type local:WidgetAViewModel}">
            <local:WidgetAView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:WidgetBViewModel}">
            <local:WidgetBView />
        </DataTemplate>
    </ItemsControl.Resources>

    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Canvas ... />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Also, you'll need to bind your Canvas.Top/Canvas.Left on the ItemContainerStyle instead of on the actual ItemTemplate to get it to display correctly in the canvas.

Upvotes: 2

Related Questions