如何创建 ListBox 项控件并将复杂类型绑定到

问题描述 投票:0回答:1

我有以下自定义控件,

public sealed class MediaFileItemControl : Control
{
    public static readonly DependencyProperty MediaFileProperty =
        DependencyProperty.Register(nameof(MediaFile), typeof(StorageFile), typeof(MediaFileItemControl), new PropertyMetadata(null));

    public MediaFileItemControl()
    {
        DefaultStyleKey = typeof(MediaFileItemControl);
    }

    public StorageFile MediaFile
    {
        get => (StorageFile)GetValue(MediaFileProperty);
        set => SetValue(MediaFileProperty, value);
    }
}

以下样式,

<!-- Generic.xaml -->
<Style TargetType="controls:MediaFileItemControl" >
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="controls:MediaFileItemControl">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{TemplateBinding MediaFile.Name}" />
                    <TextBlock Text="{TemplateBinding MediaFile.Path}" />
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

最后,我想在

ListBox
中使用它,例如:

<ListBox ItemsSource="{x:Bind ViewModel.MediaFiles, Mode=OneWay}">
    <ListBox.ItemTemplate>
        <DataTemplate x:DataType="win_storage:StorageFile" xmlns:win_storage="using:Windows.Storage">
            <controls:MediaFileItemControl MediaFile="{x:Bind}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

但是,当我构建项目时,我在

Generic.xaml

中收到以下错误
The XAML Binary Format (XBF) generator reported syntax error '0x80004005'

语法错误在哪里,实现我想要的正确方法是什么?

winui-3
1个回答
0
投票

x:Bind有很多很难克服的问题:https://github.com/microsoft/microsoft-ui-xaml/issues/2508

这篇文章很有趣:https://github.com/microsoft/microsoft-ui-xaml/issues/7810,其想法是使用指向

ContentControl
DataTemplate
。所以根据你的情况,你可以这样写:

<ResourceDictionary
    x:Class="MyWinUIApp.ResourceDictionary1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MyWinUIApp"
    xmlns:storage="using:Windows.Storage">

    <DataTemplate x:Key="StorageFileTemplate" x:DataType="storage:StorageFile">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{x:Bind Name}" />
            <TextBlock Text="{x:Bind Path}" />
        </StackPanel>
    </DataTemplate>

    <Style TargetType="local:MediaFileItemControl">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <ContentControl ContentTemplate="{StaticResource StorageFileTemplate}" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

注意,我创建了一个自定义

ResourceDictionary
,因为由于另一个神秘的原因,它在 App.xaml 中不起作用。确保您创建一个带有代码隐藏的字典来支持 x:Bind,如下所述:带有 {x:Bind} 的资源字典

如果您不想要这种复杂性,您仍然可以使用 ole

Binding
标记扩展,它通常在所谓的更好的
x:Bind
失败的地方起作用:

<Style TargetType="local:MediaFileItemControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}" />
                    <TextBlock Text="{Binding Path}" />
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
© www.soinside.com 2019 - 2024. All rights reserved.