Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I display a single item using a DataTemplate in Silverlight?

I am trying to display a single item (not contained in a collection) using a DataTemplate. Here's what I've got so far, which is displaying nothing. Replacing ItemsControl with ListBox displays an empty listbox (so I know the element is there).

        <ItemsControl
            ItemsSource="{Binding Session}"
            ItemTemplate="{StaticResource SessionHeaderDataTemplate}"
            />

Session is a single object. I want to use a DataTemplate because I am displaying the same information elsewhere in my app and wanted the presentation style defined as a resource so I can update it in one place.

Any ideas, or should I create a 1-element collection in my ViewModel and bind to that?

Edit: This is what I ended up doing, although the answer below is also a solution. I'm quite attached to my DataTemplates so didnt feel comfortable having something like this pushed out to another XAML file.

XAML:

        <ItemsControl
            DataContext="{Binding}"
            ItemsSource="{Binding Session_ListSource}"
            ItemTemplate="{StaticResource SessionHeaderDataTemplate}" />

ViewModel:

    private Session m_Session;
    public Session Session
    {
        get { return m_Session; }
        set
        {
            if (m_Session != value)
            {
                m_Session = value;
                OnPropertyChanged("Session");

                // Added these two lines 
                Session_ListSource.Clear();
                Session_ListSource.Add(this.Session);
            }
        }
    }

    // Added this property.
    private ObservableCollection<Session> m_Session_ListSource = new ObservableCollection<Session>();
    public ObservableCollection<Session> Session_ListSource
    {
        get { return m_Session_ListSource; }
        set
        {
            if (m_Session_ListSource != value)
            {
                m_Session_ListSource = value;
                OnPropertyChanged("Session_ListSource");
            }
        }
    }
like image 242
geofftnz Avatar asked Feb 18 '09 23:02

geofftnz


People also ask

What is a data template?

The data template is the method by which you communicate your request for data to the data engine. It is an XML document whose elements collectively define how the data engine will process the template to generate the XML. The data engine supports the following functionality: Single and multiple data queries.

What is DataTemplate WPF?

DataTemplate is about the presentation of data and is one of the many features provided by the WPF styling and templating model. For an introduction of the WPF styling and templating model, such as how to use a Style to set properties on controls, see the Styling and Templating topic.


2 Answers

Stick with your datatemplates for simple views with no code behind instead of having to create another user controls. Use a ContentControl to display your DataTemplate for a single item.

 <ContentControl 
      ContentTemplate="{StaticResource SessionHeaderDataTemplate}" 
      Content="{Binding Path=Session}" />
like image 198
Wallstreet Programmer Avatar answered Oct 05 '22 02:10

Wallstreet Programmer


You don't need to use an ItemsControl to do that, just create a custom user control, then bind to that, e.g.

<TestProject:myControl DataContext="{Binding Session}" />

The custom control can have it's own xaml file so it can look however you want it to.

like image 36
mattmanser Avatar answered Oct 05 '22 02:10

mattmanser