2018-8-10-win10-uwp-如何在DataTemplate绑定方法
2024-09-30 14:11:41
title | author | date | CreateTime | categories |
---|---|---|---|---|
win10 uwp 如何在DataTemplate绑定方法
|
lindexi
|
2018-08-10 19:16:50 +0800
|
2018-05-05 17:23:39 +0800
|
Win10 UWP
|
本文告诉大家几个方法在 DataTemplate 绑定。
在 DataTemplate 绑定是使用次数很多的,下面我在使用新的控件 NavigationView 就需要绑定两个按钮。
先给大家看一下界面
不要以为这个界面很复杂,实际上他需要的代码很少。
先在后台创建一个 ViewModel ,请看代码
public class ViewModel
{
public void Foo()
{
}
}
然后绑定 ViewModel ,在 MainPage 绑定就是 DataContext ,如果想知道 DataContext 的写法,请看 win10 uwp DataContext
public MainPage()
{
this.InitializeComponent(); DataContext = new ViewModel();
}
实际上界面的代码很少,请看下面
<NavigationView x:Name="NavView">
<NavigationView.HeaderTemplate>
<DataTemplate x:DataType="local:ViewModel">
<Grid Margin="24,10,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock
Margin="0,0,0,10"
VerticalAlignment="Bottom"
FontSize="28"
Style="{StaticResource TitleTextBlockStyle}"
Text="lindexi.github.io/lindexi" />
<CommandBar
Grid.Column="1"
Margin="0,0,10,0"
HorizontalAlignment="Right"
VerticalAlignment="Bottom"
DefaultLabelPosition="Right">
<AppBarButton
Icon="Edit"
Label="Feedback"
Click="{x:Bind Foo}" />
<AppBarButton
Icon="OtherUser"
Label="ChangeUser"
Click="{x:Bind Foo}" />
</CommandBar>
</Grid>
</DataTemplate>
</NavigationView.HeaderTemplate>
</NavigationView>
回到本文的问题,如何在 AppBarButton 点击绑定 ViewModel 的 Foo 函数。
实际上就是设置了 x:DataType="local:ViewModel"
就可以,方法很简单。
但是如果在那些特殊的属性绑定,如一个列表,绑定了类型是 IText 的,也就是绑定的类型可能是有多种的。除了使用列表选择还可以使用下面的方法。
x:Bind xx.DataContext.(具体类型.属性)
通过这个方式可以把 DataContext 强转为具体类型,然后拿到属性。
但是这个方法只能拿到属性,不能拿到方法。
最新文章
- MySQL 主从配置
- WORD的公式无法与文字对齐
- SharedPreferences第一次使用后HashMap将常驻内存
- 使用集成的ADT bundle来搭建android开发环境
- 中国排名前100的IT公司 (转)
- 最全的前端开发面试题及答案(js,css等等)
- Is It A Tree?(并查集)
- BroadcastReceiver接收系统广播消息
- MapReduce 开发环境搭建(Eclipse\MyEclipse + Maven)
- ios7 sdk 新特性
- Codevs 3305 水果姐逛水果街Ⅱ 倍增LCA
- 错误解决--oracle中出现ORA-01791: 不是 SELECTed 表达式 错误
- 3.MQTT paho
- HashMap 你真的了解吗?
- unity3d入门教程
- 跟我一起学opencv 第五课之调整图像亮度和对比度
- Exp3 免杀原理与实践 20164320 王浩
- Linux文件系统深度讨论【转】
- C# 流水号生成器开发
- js精度溢出解决方案