x:绑定和用户控件

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

我正在尝试通过简单的用例在UWP中使用已编译的bindings

为了使我的XAML更具可读性,易于管理,我已将DataTemplate的XAML提取到UserControl。所以我变了这个

MainPage.xaml

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <ListView ItemsSource="{x:Bind ViewModel.Items}">
        <ListView.ItemTemplate>
            <DataTemplate x:DataType="local:ProjectItem">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{x:Bind Name, Mode=OneWay}" />
                    <TextBlock Text="{x:Bind Description, Mode=OneWay}" />
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Page>

进入此

MainPage.xaml

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <ListView ItemsSource="{x:Bind ViewModel.Items}">
        <ListView.ItemTemplate>
            <DataTemplate x:DataType="local:ProjectItem">
                <local:MyUserControl1 />
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Page>

MyUserControl1.xaml

<UserControl
    x:Class="App1.MyUserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400">

    <StackPanel Orientation="Horizontal">
        <TextBlock Text="{x:Bind Name}" />
        <TextBlock Text="{x:Bind Description}" />
    </StackPanel>
</UserControl>

问题是它甚至没有编译,因为x:Bind不知道上下文。

x:Bind如何涵盖此用例?

c# xaml uwp win-universal-app
3个回答
0
投票

如果使用这种方法,则可以(或需要)向MyUserControl1.xaml.cs添加一个属性,该属性将当前DataContext强制转换为ProjectItem并返回它:

public ProjectItem Item => DataContext as ProjectItem;

public MyUserControl1()
{
    InitializeComponent();
    DataContextChanged += (s, e) => Bindings.Update();
}

然后您在XAML标记中绑定到此属性:

<StackPanel Orientation="Horizontal">
    <TextBlock Text="{x:Bind Item.Name}" />
    <TextBlock Text="{x:Bind Item.Description}" />
</StackPanel>

另一种选择是使用未编译的{Bindings}或摆脱MyUserControl1并返回到内联DataTemplate


0
投票

几周前我遇到了同样的问题。 x:Bind无效,但我将其更改为Text="{Binding Path = Name, ElementName=YourControlName}"。为此,您需要命名您的UserControl。只需在行Name="YourControlName"之后添加d:DesignWidth="400"


0
投票

我建议在MyUserControl1.xaml.cs上为ProjectItem创建依赖项属性

  public static readonly DependencyProperty ProjectItemProperty =
        DependencyProperty.Register(
            nameof(ProjectItem),
            typeof(ProjectItem),
            typeof(MyUserControl1),
            null);

    public ProjectItem ProjectItem
    {
        get => (ProjectItem)GetValue(ProjectItemProperty);
        set => SetValue(ProjectItemProperty, value);
    }

然后在XAML上,绑定ProjectItem依赖项属性的属性:

<StackPanel Orientation="Horizontal">
    <TextBlock Text="{x:Bind ProjectItem.Name, Mode=OneWay}" />
    <TextBlock Text="{x:Bind ProjectItem.Description, Mode=OneWay}" />
</StackPanel>

然后在MainPage.xaml上传递“ ProjectItem”集合项。

<DataTemplate x:DataType="local:ProjectItem">
            <local:MyUserControl1 ProjectItem="{x:Bind}"/>
        </DataTemplate>
© www.soinside.com 2019 - 2024. All rights reserved.