设为首页 收藏本站
查看: 684|回复: 0

[经验分享] 背水一战 Windows 10 (18)

[复制链接]

尚未签到

发表于 2017-6-29 10:47:17 | 显示全部楼层 |阅读模式
  [源码下载]




背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue  
作者:webabcd

介绍
背水一战 Windows 10 之 绑定


  • 与 Element 绑定
  • 与 Indexer 绑定
  • TargetNullValue - 当绑定数据为 null 时显示的值
  • FallbackValue - 当绑定失败时显示的值
  
示例
1、演示如何与 Element 绑定
Bind/BindingElement.xaml



<Page
x:Class="Windows10.Bind.BindingElement"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Bind"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="Transparent">
<StackPanel Margin="10 0 10 10">
<!--
本例用于演示如何与 Element 绑定,以及 OneTime, OneWay, TwoWay 的区别
-->
<!--
OneTime 方式绑定元素
-->
<TextBox Text="{Binding ElementName=txtOneTime, Path=Text, Mode=OneTime}" />
<TextBox Name="txtOneTime" Text="OneTime" Margin="0 10 0 0" />

<!--
OneWay 方式绑定元素(OneWay 是默认方式)
-->
<TextBox Text="{Binding ElementName=txtOneWay, Path=Text, Mode=OneWay}" Margin="0 30 0 0" />
<TextBox Name="txtOneWay" Text="OneWay" Margin="0 10 0 0" />
<!--
TwoWay 方式绑定元素(同时演示一下 Binding 标记的另一种写法,就是直接把 Path 指定的路径放到 Binding 的后面)
-->
<TextBox Text="{Binding Text, ElementName=txtTwoWay, Mode=TwoWay}" Margin="0 30 0 0" />
<TextBox Name="txtTwoWay" Text="TwoWay" Margin="0 10 0 0" />
<!--
TwoWay 方式绑定元素(在 C# 端指定 Binding 对象的方式一)
-->
<TextBox Name="textBox1" Margin="0 30 0 0" />
<TextBox Name="textBox2" Text="TwoWay" Margin="0 10 0 0" />
<!--
TwoWay 方式绑定元素(在 C# 端指定 Binding 对象的方式二)
-->
<TextBox Name="textBox3" Margin="0 30 0 0" />
<TextBox Name="textBox4" Text="TwoWay" Margin="0 10 0 0" />
</StackPanel>
</Grid>
</Page>
  Bind/BindingElement.xaml.cs



/*
* 演示如何与 Element 绑定
*/
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
namespace Windows10.Bind
{
public sealed partial class BindingElement : Page
{
public BindingElement()
{
this.InitializeComponent();
SetBindingDemo1();
SetBindingDemo2();
}
// 在 C# 端做绑定(方式一)
private void SetBindingDemo1()
{
// 实例化 Binding 对象
Binding binding = new Binding()
{
ElementName = nameof(textBox2),
Path = new PropertyPath(nameof(TextBox.Text)),
Mode = BindingMode.TwoWay // 默认是 OneWay 的
            };
// 将目标对象的目标属性与指定的 Binding 对象关联
            BindingOperations.SetBinding(textBox1, TextBox.TextProperty, binding);
}
// 在 C# 端做绑定(方式二)
private void SetBindingDemo2()
{
// 实例化 Binding 对象
Binding binding = new Binding()
{
ElementName = nameof(textBox4),
Path = new PropertyPath(nameof(TextBox.Text)),
Mode = BindingMode.TwoWay // 默认是 OneWay 的
            };
// 将目标对象的目标属性与指定的 Binding 对象关联
            textBox3.SetBinding(TextBox.TextProperty, binding);
}
}
}
  
2、演示如何与 Indexer 绑定
Bind/BindingIndexer.xaml



<Page
x:Class="Windows10.Bind.BindingIndexer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Bind"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="Transparent">
<StackPanel Margin="10 0 10 10">
<!--演示如何绑定集合中的某个元素-->
<TextBlock Name="textBlock" Text="{Binding Path=[3]}" />
<!--演示如何绑定集合中的某个对象的某个属性-->
<TextBlock Name="textBlock2" Text="{Binding Path=[5].Name}" Margin="0 10 0 0" />
<!--演示如何绑定 string 类型的索引器-->
<TextBlock Name="textBlock3" Text="{Binding Path=[webabcd]}" Margin="0 10 0 0" />
<!--演示如何绑定字典表中指定 key 的数据-->
<TextBlock Name="textBlock4" Text="{Binding Path=[hello]}" Margin="0 10 0 0" />
<!--演示如何在 C# 端绑定索引器-->
<TextBox Name="textBox" Margin="0 10 0 0" />
</StackPanel>
</Grid>
</Page>
  Bind/BindingIndexer.xaml.cs



/*
* 演示如何与 Indexer 绑定
*/
using System;
using System.Collections.Generic;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows10.Common;
namespace Windows10.Bind
{
public sealed partial class BindingIndexer : Page
{
public BindingIndexer()
{
this.InitializeComponent();
this.Loaded += BindingIndexer_Loaded;
BindingDemo();
}
private void BindingIndexer_Loaded(object sender, RoutedEventArgs e)
{
// 用于演示如何绑定集合中的某个元素
List<string> list = new List<string>();
for (int i = 0; i < 10; i++)
{
list.Add("索引:" + i.ToString());
}
textBlock.DataContext = list;
// 用于演示如何绑定集合中的某个对象的某个属性
textBlock2.DataContext = TestData.GetEmployees();
// 用于演示如何绑定 string 类型的索引器
textBlock3.DataContext = this;
// 用于演示如何绑定字典表中指定 key 的数据
Dictionary<string, string> dic = new Dictionary<string, string>() { { "hello", "hello webabcd" } };
textBlock4.DataContext = dic;
}
// 自定义一个索引器
public object this[string indexer]
{
get
{
return "string: " + indexer;
}
}

// 在 C# 端绑定索引器
private void BindingDemo()
{
textBox.DataContext = this;
// 实例化 Binding 对象
Binding binding = new Binding()
{
Path = new PropertyPath("[wanglei]")
};
// 将目标对象的目标属性与指定的 Binding 对象关联
            BindingOperations.SetBinding(textBox, TextBox.TextProperty, binding);
/*
* 注:经测试在 TextBox 做如上绑定是正常的。但是如果在 TextBlock 做如上绑定则运行时报错 Attempted to read or write protected memory. This is often an indication that other memory is corrupt. 不知道为什么
*/
}
}
}
  
3、演示 Binding 中的 TargetNullValue 和 FallbackValue 的用法
Bind/TargetNullValueFallbackValue.xaml



<Page
x:Class="Windows10.Bind.TargetNullValueFallbackValue"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Bind"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="Transparent">
<StackPanel Margin="10 0 10 10">
<!--
FallbackValue - 当绑定失败时显示的值
-->
<TextBlock Name="textBlock1" Text="{Binding Path=MyName, FallbackValue='绑定失败时的默认值'}" Margin="5" />
<!--
TargetNullValue - 当绑定数据为 null 时显示的值
-->
<TextBlock Name="textBlock2" Text="{Binding Path=MyName, TargetNullValue='绑定数据的返回值为 null'}" Margin="5" />
</StackPanel>
</Grid>
</Page>
  Bind/TargetNullValueFallbackValue.xaml.cs



/*
* 演示 Binding 中的 TargetNullValue 和 FallbackValue 的用法
*/
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace Windows10.Bind
{
public sealed partial class TargetNullValueFallbackValue : Page
{
public TargetNullValueFallbackValue()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// 为 textBlock2 提供数据上下文
textBlock2.DataContext = this;
/*
// 实例化 Binding 对象
Binding binding = new Binding()
{
Path = new PropertyPath("Name"),
TargetNullValue = "TargetNullValue",
FallbackValue = "FallbackValue"
};
// 将目标对象的目标属性与指定的 Binding 对象关联
BindingOperations.SetBinding(textBlock2, TextBox.TextProperty, binding);
*/
}
public string MyName { get; set; } = null;
}
}
  
OK
[源码下载]

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-389250-1-1.html 上篇帖子: [windows][C++][库]遍历删除文件夹 下篇帖子: Windows 10 X64 ISO 专业版&家庭版下载与永久激活
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表