那麼SL4中有沒有可用的東西?ListBox和DataGrid的silverlight模板何時爲空?
我需要顯示某種信息的時候沒有數據呈現在ListBox和/或數據網格,當他們是空的。
如果有人熟悉任何這些並能提供實例或鏈接,我將不勝感激。
感謝,
巫毒
那麼SL4中有沒有可用的東西?ListBox和DataGrid的silverlight模板何時爲空?
我需要顯示某種信息的時候沒有數據呈現在ListBox和/或數據網格,當他們是空的。
如果有人熟悉任何這些並能提供實例或鏈接,我將不勝感激。
感謝,
巫毒
我沒有嘗試這樣做我自己,但你可能有興趣在下面的博客文章的鏈接,它提供了爲DataGrid,你很可能適應的解決方案列表框也是如此。
http://subodhnpushpak.wordpress.com/2009/05/18/empty-data-template-in-silverlight-datagrid/
我來爲列表框一個簡單的解決方案,在99%的情況對我的作品。一旦設置爲資源,您只需更改列表框上的標籤屬性即可獲取所有功能。
首先,我修改列表框的默認模板包括一個新的格柵和一個文本框,如下所示:
原始XAML
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3" Margin="0">
<ScrollViewer x:Name="ScrollViewer" Background="Transparent" BorderBrush="Transparent" BorderThickness="0" Margin="0" Padding="0" TabNavigation="{TemplateBinding TabNavigation}">
<ItemsPresenter Margin="0,0" />
</ScrollViewer>
</Border>
新XAML
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3" Margin="0">
<Grid >
<TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag}" VerticalAlignment="Center" HorizontalAlignment="Center" Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ItemsSource.Count, Converter={StaticResource ListCount2Visibility}}" Foreground="{StaticResource NormalFontBrush}" FontSize="{StaticResource DefaultFontSize}" />
<ScrollViewer x:Name="ScrollViewer" Background="Transparent" BorderBrush="Transparent" BorderThickness="0" Margin="0" Padding="0" TabNavigation="{TemplateBinding TabNavigation}">
<ItemsPresenter Margin="0,0" />
</ScrollViewer>
</Grid>
</Border>
文本塊可見性屬性綁定到一個自定義轉換器有權ListCount2Visibility看起來像這樣:如果
public sealed class ListCount2Visibility : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null && (int)value > 0)
return "Collapsed";
else
return "Visible";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
值變換器檢查ItemSource.Count == 0 ...如果它是,它設置能見度可見。否則,它會崩潰。
新TextBlock的文本屬性然後綁定到列表框的標籤財產。 (這並不理想,但它是將文本放入控件中的最快方式。顯然,如果您使用標籤屬性來處理其他內容,這將不起作用)。
因此,基本上,你設置的標籤要顯示的消息,並隨時有列表中沒有的項目,在文本框中顯示(水平和垂直居中)。在開發過程中,您的消息將顯示,因爲列表是空的(假設現在是設計時間datacontext),這使得很好地顯示文本。
這真的是所有的一切。
如果你願意,你甚至可以在列表框的標籤屬性綁定到你的視圖模型更改文本。因此,當從數據庫返回項目時,可以執行諸如「加載....」之類的操作,然後在加載完所有內容後將其更改爲「空列表」消息。 (當然忙指標可能會更好)