WP7 相当于 EmptyDataTemplate?

发布于 2024-10-20 15:47:11 字数 172 浏览 1 评论 0原文

许多 ASP.NET 数据绑定控件公开一个 EmptyDataTemplate,该模板在控件绑定到空数据源时呈现。在我的 WP7 应用程序中,我也想在绑定到 ListBox 的数据源为空时显示一条友好的消息。有没有一种相当优雅的方法来实现这一目标?最好与 calibburn.micro 集成/能够集成?

谢谢!!

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 message 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!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

微凉 2024-10-27 15:47:11

我不喜欢使用隐藏代码来实现这样的功能。我宁愿建议实现一个可在绑定标记中使用的 DataTemplateConverter 来实现这个确切的功能。

例如:

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

转换器将在 xaml 文件的资源部分中实例化。

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

在这种情况下,Empty/NonEmpty 实现由您决定。

要了解如何实现此类 ValueConverter,请参阅 MSDN(或 google)

已添加示例。您可以使用 DataTemplate 的依赖属性,但为了简洁起见,我在这里省略了这一点。

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();
    }
}

编辑:
实现相同目标的其他方式,但“Silverlight 方式”多一点。使用 GoToStateAction 和适当的触发器。将模板图形封装在 UserControl 中,并指定此 UserControl 的状态。这样,用户控件将根据触发器的行为(空/非空)而改变。

结果将与我之前的提议相同,但具有状态更改动画的额外好处,这很难使用 DataTemplateConverter 实现(修改后的 TransitioningContentControl)。

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.

金兰素衣 2024-10-27 15:47:11

不确定 caliburn.micro,但例如,如果您要绑定到 ObservableCollection (在我看来,绑定到任何东西的最佳集合),则有 CollectionChanged< /code> 事件处理程序。

也就是说:

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

在这里,在事件处理程序本身中,您可以检查触发集合是否为空:

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

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
    }
}
万水千山粽是情ミ 2024-10-27 15:47:11

Silverlight 中不存在这样的现成功能。

但是,您可以做的是使用正确的消息创建一个 TextBlock ,并使用转换器将其可见性与 ListBoxItemsSource 绑定。当Count > 时,该转换器应返回Visibility.Visible。当 Count == 0 时,为 0Visibility.Collapsed

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文