silverlight
发布时间:2022-05-04 发布网站:大佬教程 code.js-code.com
大佬教程收集整理的这篇文章主要介绍了稳扎稳打Silverlight(50) - 4.0绑定之DependencyObject绑定, 索引器绑定, StringFormat, TargetNullValue和FallbackValue,,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
概述
[源码下载] 稳扎稳打Silverlight(50) - 4.0绑定之DependencyOb
ject绑定, 索引器绑定,
StringFormat, TargetNullValue和
fall
BACkValue, CollectionView
source 作者: webabcd 介绍 Silverlight 4.0 绑定相关的增强: DependencyOb
ject Binding - 新增了
[源码下载]
稳扎稳打Silverlight(50) - 4.0绑定之DependencyOb
ject绑定,索引器绑定,
StringFormat,TargetNullValue和
fall
BACkValue,CollectionView
source
作者:
webabcd
介绍
Silverlight 4.0 绑定相关的增强:
- DependencyObject Binding - 新增了对 DependencyObject 绑定的支持
- Indexer Binding - 新增了对索引器绑定的支持
- StringFormat - 指定绑定数据的显示格式
- TargetNullValue - 当绑定数据为 null 时所需要显示的值
- fallBACkValue - 当绑定失败(无法返回值)的时候所需要显示的值
- CollectionViewsource - 实现了 ICollectionView 的类,可以通过它对数据排序、筛选和分组
在线DEMO
http://www.cnblogs.com/webabcd/archive/2010/08/09/1795417.html
示例
1、演示如何绑定到 DependencyOb
ject
DependencyOb
jectBinding.xaml
代码
@H_
502_48@
<
navigation:Page
x:Class
="Silverlight40.binding.DependencyObjectBinding"
xmlns
="http://scheR_428_11845@as.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://scheR_428_11845@as.microsoft.com/winfx/2006/xaml"
xmlns:d
="http://scheR_428_11845@as.microsoft.com/expression/blend/2008"
xmlns:mc
="http://scheR_428_11845@as.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation
="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
title
="DependencyObjectBinding Page"
>
<
Grid
x:Name
="LayoutRoot"
>
<
StackPanel
HorizontalAlignment
="Left"
>
<!--
Silverlight 3.0 支持绑定到 FrameworkElement
textBox 继承自 FrameworkElement
-->
<
TextBox
Text
="
{Binding ELEMENTNAME=slider, Path=value}
"
/>
<!--
Silverlight 4.0 中新增了对 DependencyObject 绑定的支持
rotateTransform 继承自 DependencyObject
-->
<
Rectangle
Width
="100"
Height
="100"
renderTransformOrigin
="0.5, 0.5"
Fill
="Red"
>
<
Rectangle.RenderTransform
>
<
RotateTransform
Angle
="
{Binding ELEMENTNAME=slider, Path=value}
"
/>
</
Rectangle.RenderTransform
>
</
Rectangle
>
<
Slider
Name
="slider"
Height
="20"
Minimum
="0"
Maximum
="360"
/>
</
StackPanel
>
</
Grid
>
</
navigation:Page
>
2、演示如何绑定到索引器
IndexerBinding.xaml
代码
<
navigation:Page
x:Class
="Silverlight40.binding.IndexerBinding"
xmlns
="http://scheR_428_11845@as.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://scheR_428_11845@as.microsoft.com/winfx/2006/xaml"
xmlns:d
="http://scheR_428_11845@as.microsoft.com/expression/blend/2008"
xmlns:mc
="http://scheR_428_11845@as.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation
="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
title
="IndexerBinding Page"
>
<
Grid
x:Name
="LayoutRoot"
>
<!--
用于演示索引器的绑定
-->
<
TextBlock
Name
="textBlock"
text
="
{Binding Path=[3] }
"
/>
</
Grid
>
</
navigation:Page
>
IndexerBinding.xam
l.cs
代码
/*
* Silverlight 4.0 中新增了对索引器绑定的支持,索引的类型必须实现 IList
*/
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Net;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Documents;
using
System.Windows.Input;
using
System.Windows.Media;
using
System.Windows.Media.Animation;
using
System.Windows.Shapes;
using
System.Windows.Navigation;
namespace
Silverlight40.binding
{
public
partial
class
indexerBinding : Page
{
public
indexerBinding()
{
initializeComponent();
}
protected
override
void
OnNavigatedTo(NavigationEventArgs E)
{
List
<
String
>
list
=
new
List
<
String
>
();
for
(
int
i
=
0
; i
<
10
; i
++
)
{
list.Add(
"
索引:
"
+
i.ToString());
}
textBlock.DataCo@R_262_10443@t
=
list;
}
}
}
3、演示在绑定时使用
StringFormat 来指定数据的
显示格式
StringFormat.xaml
代码
<
navigation:Page
x:Class
="Silverlight40.binding.StringFormat"
xmlns
="http://scheR_428_11845@as.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://scheR_428_11845@as.microsoft.com/winfx/2006/xaml"
xmlns:d
="http://scheR_428_11845@as.microsoft.com/expression/blend/2008"
xmlns:mc
="http://scheR_428_11845@as.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation
="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
title
="StringFormat Page"
>
<
Grid
x:Name
="LayoutRoot"
>
<!--
StringFormat - 指定绑定数据的显示格式
-->
<
TextBlock
Name
="textBlock"
text
="
{Binding StringFormat='yyyy-MM-dd HH:mm:ss'}
"
/>
</
Grid
>
</
navigation:Page
>
StringFormat.xam
l.cs
代码
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Net;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Documents;
using
System.Windows.Input;
using
System.Windows.Media;
using
System.Windows.Media.Animation;
using
System.Windows.Shapes;
using
System.Windows.Navigation;
namespace
Silverlight40.binding
{
public
partial
class
StringFormat : Page
{
public
StringFormat()
{
initializeComponent();
}
protected
override
void
OnNavigatedTo(NavigationEventArgs E)
{
textBlock.DataCo@R_262_10443@t
=
datetiR_428_11845@e.Now;
}
}
}
4、演示 TargetNullValue 和
fall
BACkValue 的
效果
TargetNullValue
fall
BACkValue.xaml
代码
<
navigation:Page
x:Class
="Silverlight40.binding.TargetNullValuefallBACkValue"
xmlns
="http://scheR_428_11845@as.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://scheR_428_11845@as.microsoft.com/winfx/2006/xaml"
xmlns:d
="http://scheR_428_11845@as.microsoft.com/expression/blend/2008"
xmlns:mc
="http://scheR_428_11845@as.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation
="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
title
="TargetNullValuefallBACkValue Page"
>
<
Grid
x:Name
="LayoutRoot"
>
<
StackPanel
HorizontalAlignment
="Left"
NAME
="stackPanel"
>
<!--
fallBACkValue - 当绑定失败(无法返回值)的时候所需要显示的值
-->
<
TextBlock
Text
="
{Binding Path=xxx, fallBACkValue='绑定失败时的默认值'}
"
/>
<!--
TargetNullValue - 当绑定数据为 null 时所需要显示的值
-->
<
TextBlock
Text
="
{Binding TargetNullValue='绑定返回值为 null'}
"
/>
</
StackPanel
>
</
Grid
>
</
navigation:Page
>
TargetNullValue
fall
BACkValue.xam
l.cs
代码
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Net;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Documents;
using
System.Windows.Input;
using
System.Windows.Media;
using
System.Windows.Media.Animation;
using
System.Windows.Shapes;
using
System.Windows.Navigation;
namespace
Silverlight40.binding
{
public
partial
class
TargetNullValuefallBACkValue : Page
{
public
TargetNullValuefallBACkValue()
{
initializeComponent();
}
protected
override
void
OnNavigatedTo(NavigationEventArgs E)
{
stackPanel.DataCo@R_262_10443@t
=
null
;
}
}
}
5、演示 CollectionView
source 的应用
Product.cs
代码
using
System;
using
System.Net;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Documents;
using
System.Windows.Ink;
using
System.Windows.Input;
using
System.Windows.Media;
using
System.Windows.Media.Animation;
using
System.Windows.Shapes;
namespace
Silverlight40.binding
{
//
实体类
public
class
Product
{
public
int
ProductId {
get
;
set
; }
public
String
NAME {
get
;
set
; }
public
String
Category {
get
;
set
; }
}
}
ProductCollection.cs
代码
using
System;
using
System.Net;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Documents;
using
System.Windows.Ink;
using
System.Windows.Input;
using
System.Windows.Media;
using
System.Windows.Media.Animation;
using
System.Windows.Shapes;
using
System.Collections.Generic;
namespace
Silverlight40.binding
{
public
class
ProductCollection : List
<
Product
>
{
}
}
CollectionView
source.xaml
代码
<
navigation:Page
x:Class
="Silverlight40.binding.CollectionViewsource"
xmlns
="http://scheR_428_11845@as.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://scheR_428_11845@as.microsoft.com/winfx/2006/xaml"
xmlns:d
="http://scheR_428_11845@as.microsoft.com/expression/blend/2008"
xmlns:mc
="http://scheR_428_11845@as.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation
="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
xmlns:sdk
="http://scheR_428_11845@as.microsoft.com/winfx/2006/xaml/presentation/sdk"
xmlns:c
="clr-namespace:System.ComponentModel;assembly=System.Windows"
xmlns:local
="clr-namespace:Silverlight40.binding"
title
="CollectionViewsource Page"
>
<
Grid
x:Name
="LayoutRoot"
>
<
Grid.resources
>
<
local:ProductCollection
x:Key
="products"
>
<
local:Product
ProductId
="1"
NAME
="abc"
Category
="CategoryA"
/>
<
local:Product
ProductId
="2"
NAME
="xyz"
Category
="CategoryA"
/>
<
local:Product
ProductId
="3"
NAME
="webabcd"
Category
="CategoryB"
/>
</
local:ProductCollection
>
<!--
CollectionViewsource - 实现了 ICollectionView 的类,可以通过它对数据排序、筛选和分组
CollectionViewsource.sortDescriptions - 指定排序方式
PropertyName - 指定排序的字段
Direction - 指定按升序还是降序排序
CollectionViewsource.GroupDescriptions - 指定分组方式
PropertyName - 指定分组的字段
StringComparison - 指定是否区分大小写等
-->
<
CollectionViewsource
x:Name
="datasource"
source
="
{Staticresource products}
"
>
<
CollectionViewsource.sortDescriptions
>
<
c:SortDescription
PropertyName
="Name"
Direction
="Descending"
/>
</
CollectionViewsource.sortDescriptions
>
<
CollectionViewsource.GroupDescriptions
>
<
PropertyGroupDescription
PropertyName
="Category"
StringComparison
="CurrentCultureIgnoreCase"
/>
</
CollectionViewsource.GroupDescriptions
>
</
CollectionViewsource
>
</
Grid.resources
>
<
sdk:DataGrid
Itemssource
="
{Binding source={Staticresource datasourcE}}
"
/>
</
Grid
>
</
navigation:Page
>
OK
[源码下载]
大佬总结
以上是大佬教程为你收集整理的稳扎稳打Silverlight(50) - 4.0绑定之DependencyObject绑定, 索引器绑定, StringFormat, TargetNullValue和FallbackValue,全部内容,希望文章能够帮你解决稳扎稳打Silverlight(50) - 4.0绑定之DependencyObject绑定, 索引器绑定, StringFormat, TargetNullValue和FallbackValue,所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。