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 强转为具体类型,然后拿到属性。

但是这个方法只能拿到属性,不能拿到方法。

最新文章

  1. MySQL 主从配置
  2. WORD的公式无法与文字对齐
  3. SharedPreferences第一次使用后HashMap将常驻内存
  4. 使用集成的ADT bundle来搭建android开发环境
  5. 中国排名前100的IT公司 (转)
  6. 最全的前端开发面试题及答案(js,css等等)
  7. Is It A Tree?(并查集)
  8. BroadcastReceiver接收系统广播消息
  9. MapReduce 开发环境搭建(Eclipse\MyEclipse + Maven)
  10. ios7 sdk 新特性
  11. Codevs 3305 水果姐逛水果街Ⅱ 倍增LCA
  12. 错误解决--oracle中出现ORA-01791: 不是 SELECTed 表达式 错误
  13. 3.MQTT paho
  14. HashMap 你真的了解吗?
  15. unity3d入门教程
  16. 跟我一起学opencv 第五课之调整图像亮度和对比度
  17. Exp3 免杀原理与实践 20164320 王浩
  18. Linux文件系统深度讨论【转】
  19. C# 流水号生成器开发
  20. js精度溢出解决方案

热门文章

  1. 通过button将form表单的数据提交到action层
  2. 根据网站运行日志猜测的百度蜘蛛ip
  3. 如何确定要对DIV设置什么CSS属性样式呢?
  4. python基础--模块的查找顺序以及相对导入和绝对导入
  5. Zookeeper安装过程
  6. 2019-8-31-dotnet-Framework-源代码-类库的意思
  7. 【模板】tyvjP1520 树的直径 [2017年5月计划 清北学堂51精英班Day3]
  8. yield函数的执行顺序
  9. 2019阿里云开年Hi购季新用户分会场全攻略!
  10. line-height的用法(一)