繁体   English   中英

x:Bind in WinUI3 绑定到日期时间

[英]x:Bind in WinUI3 Binding to a datetime

我正在尝试将当前 DateTime 绑定到 WinUI3 中的文本块对象。

<Page
    x:Class="HabitTracker.Screens.HomePage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:HabitTracker.Screens"
    xmlns:sys="using:System"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid> 
      <Grid>
        <GridView MaxHeight="500" ItemsSource="{x:Bind Habits}">
          <GridView.HeaderTemplate>
           <DataTemplate>
            <StackPanel Orientation="Horizontal" Spacing="45">
             <TextBlock Text="{x:Bind sys:DateTime.Now}" />
             <TextBlock Text="hello2" />
             <TextBlock Text="hello2" />
             <TextBlock Text="hello2" />
            <TextBlock Text="hello2" />
         </StackPanel>
       </DataTemplate>
    </GridView.HeaderTemplate>

...但是我的绑定不起作用。 运行应用程序时,我在 UI 上得到空字符串。

我有以下命名空间声明:

xmlns:sys="using:System"

我错过了什么?

注意:我还阅读了此文档https://docs.microsoft.com/en-us/windows/uwp/data-binding/function-bindings ,尽管它适用于 UWP,而不是 WinUi3 文档。

我正在使用 WindowsAppSDK 1.1.1

将 Textblock 放入ListView.HeaderTemplateDataTemplate时,我可以重现您的行为。 我不确定原因,但我怀疑该行为与DataContext有关。 当您将TextBlock放入DataTemplate时, DataContext将更改为 GridView 模板的根控件。

我使用的一种解决方法是将TextBlock放入UserControl而不是直接将TextBlock放入DataTemplate

用户控制:

 <StackPanel>
    <TextBlock Text="{x:Bind sys:DateTime.Now}" />
</StackPanel>

页:

 <ListView x:Name="MyListView" Width="500" Height="500" >
        <ListView.HeaderTemplate>
            <DataTemplate >
                <StackPanel>
                    <local:UserControl1 />
                    <TextBlock Text="Hello"/>
                    <TextBlock Text="Hello"/>
                    <TextBlock Text="Hello"/>
                    <TextBlock Text="Hello"/>
                </StackPanel>
            </DataTemplate>
        </ListView.HeaderTemplate>
        <x:String >123</x:String>
        <x:String >123</x:String>
        <x:String >123</x:String>
        <x:String >123</x:String>
    </ListView>

它看起来像什么:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM