开发者

WP7 equivalent of EmptyDataTemplate?

开发者 https://www.devze.com 2023-02-14 21:43 出处:网络
A number of ASP.NET data bound controls expose an EmptyDataTemplate that is rendered when the control is bound to an empty data source. In my WP7 app, I too would like to display a friendly messag开发

A number of ASP.NET data bound controls expose an EmptyDataTemplate that is rendered when the control is bound to an empty data source. In my WP7 app, I too would like to display a friendly messag开发者_运维知识库e when the data source that is bound to the ListBox is empty. Is there a reasonably elegant way to achieve this? Preferably integrated/able with caliburn.micro?

Thanks!!


I don't like using code behind for such a functionality. I would rather recommend implementing a DataTemplateConverter useable in the binding markup to achieve this exact functionality.

for exemple:

<ContentControl ContentTemplate="{Binding Converter={StaticResource templateConverter}, Path=yourbindingpath}"/>

the converter would be instantiated in the resource section of the xaml file.

<myControls:EmptyDataTemplateConverter x:Key="templateConverter">
  <myControls:EmptyDataTemplateConverter.NonEmpty>
     <DataTemplate>[...]</DataTemplate>
  </myControls:EmptyDataTemplateConverter.NonEmpty>
  <myControls:EmptyDataTemplateConverter.Empty>
     <DataTemplate>[...]</DataTemplate>
  </myControls:EmptyDataTemplateConverter.Empty>
</myControls:EmptyDataTemplateConveter>

In this case, the Empty/NonEmpty implementation is up to you.

To understand how you can implement such a ValueConverter, see MSDN (or google)

Sample added. You could use dependency properties for the DataTemplate, but for brievty I ometted this here.

public class EmptyDataTemplateConverter: IValueConverter
{
    public DataTemplate Empty{get;set;}
    public DataTemplate NonEmpty{get;set;}

    // This converts the DateTime object to the DataTemplate to use.
    public object Convert(object value, Type targetType, object parameter,
    System.Globalization.CultureInfo culture)
   {
       if(IsEmpty(value))
       {
          return this.Empty;
       }
       else
       {
          return this.NonEmpty;
       }
   }

    //Your "empty/not empty" implementation here. Mine is rather... incomplete.
    private bool IsEmpty(object value)
    {
       return value!=null;
    }
    // No need to implement converting back on a one-way binding 
    public object ConvertBack(object value, Type targetType, 
        object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Edit: Other way of achieving the same goal, but a little more in the "Silverlight way". Use a GoToStateAction and the adequate trigger. Encapsulate your template graphics in an UserControl, and specify States for this UserControl. This way, the user control will change according to the trigger's behavior (Empty/not empty).

The result will be the same as my former proposition, but with the added benefit of state changes animations, which would be difficult to achieve (modified TransitioningContentControl) with a DataTemplateConverter.


Not sure about caliburn.micro, but for example, if you are binding to an ObservableCollection<T> (in my opinion, the best collection to bind to anything), there is the CollectionChanged event handler.

So to say:

ObservableCollection<string> c = new ObservableCollection<string>();
c.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(c_CollectionChanged);

Here, in the event handler itself you can check whether the triggering collection is empty or not:

void c_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
    if (((ObservableCollection<string>)sender).Count == 0)
    {
        // Action here
    }
}


There is no such functionality out of the box in Silverlight.

What you can do however is create a TextBlock with a proper message and bind it's visibility with the ListBox's ItemsSource using a converter. That converter should return Visibility.Visible when Count > 0 and Visibility.Collapsed when Count == 0.

0

精彩评论

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

关注公众号