English 中文(简体)
WinUI 3 How to make DataTemplate usable in all pages?
原标题:

WinUI 3 c++/winrt. The DataTemplate is listed at the top of the page:

<Page.Resources>
    <DataTemplate x:Key="SomeViewTemplate" x:DataType="local:SomeModel">
        <StackPanel>
            <TextBlock Text="{x:Bind val1}"/>
            <RatingControl MaxRating="{x:Bind val2}" />
        </StackPanel>
    </DataTemplate>
</Page.Resources>

Everything works fine on this page. How can I make this DataTemplate usable on other pages? In what place (file) should it be placed?

问题回答

Let s say you have your DataTemplate in a ResourceDictionary "DataTemplates.xaml". Note that you need to use Binding instead of x:Bind.

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:DataTemplateExample">

     <DataTemplate x:Key="SomeViewTemplate">
         <StackPanel>
             <TextBlock Text="{Binding val1}"/>
             <RatingControl MaxRating="{Binding val2}" />
         </StackPanel>
     </DataTemplate>

</ResourceDictionary>

Add it to App.xaml,

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
            <!--  Other merged dictionaries here  -->
            <ResourceDictionary Source="DataTemplates.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <!--  Other app resources here  -->
    </ResourceDictionary>
</Application.Resources>

Then you should be able to use it.

<ListView
    ItemTemplate="{StaticResource SomeViewTemplate}"
    ItemsSource="{x:Bind Items, Mode=OneWay}" />

When I placed DataTemplate in App.xaml or a separate ResourceDictionary as @AndrewKeepCoding answered, a Xaml Compiler error at x:DataType, XamlCompiler error WMC0612: The XAML Binary Format (XBF) generator reported syntax error 0x09C4 : Property Not Found, was raised. However, It s necessary to specify x:DataType in order to make the DataTemplate take effect.

So, my conclusion is DataTemplate cannot be global in c++ and DataTemplate also states limited properties to use.

Binding to a C++/WinRT collection costs me a long time until I find PhotoEditor sample. Must specify IInspectable instead of a custom runtime class name.

enter image description here

import "BookSku.idl";

namespace App1
{
    [default_interface]
    runtimeclass MainWindow : Microsoft.UI.Xaml.Window
    {
        MainWindow();
        Int32 MyProperty;
        Windows.Foundation.Collections.IVector<IInspectable> BookSkus{ get; };
    }
}

enter image description here





相关问题
How to display all installed fonts in a ComboBox

I need to display all the user s installed fonts in a WinUI 3 ComboBox. I m using this code: <ComboBox x:Name="FontComboBox" ItemsSource="{Binding Source={x:Static Fonts....

图3

我似乎不能让我的WinUI 3 Button展示形象。

Can t use WinUI 3个Picker

I m试图在我的Windows App SDK(WinUI 3)申请中展示一个卷宗。 I m 将其列入C#。

热门标签