我可以使用 x:Bind 从 DataTemplate 中绑定到视图模型吗?

问题描述 投票:0回答:2
uwp uwp-xaml winui-3
2个回答
1
投票

如果

DataTemplate
Page
的资源,您可以
x:Name
您的
Page
,例如ThisPage,然后:

<DataTemplate>
    <TextBlock Text="{Binding ElementName=ThisPage, Path=ViewModel.AwesomeString, Mode=OneWay}" />
</DataTemplate>

但是:

  • 适用于
    Binding
    ,但不适用于
    x:Bind
    ,因此无法调用方法。
  • 适用于
    ListView
    GridView
    ,但不适用于
    ItemsRepeater

现在,让我向您展示一个使用值转换器的示例。此示例依赖于

Foo
string
或可以使用
ToString()
,但您也许可以将其应用到您的案例中。

public class  StringToColorDictionary : Dictionary<string, Color>
{
}
public class ObjectToColorConverter : IValueConverter
{
    public StringToColorDictionary Colors { get; set; } = new();

    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return
            value.ToString() is string stringValue &&
            Colors.TryGetValue(stringValue, out var color)
                ? new SolidColorBrush(color)
                : new SolidColorBrush(Microsoft.UI.Colors.Transparent);
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}
<Page.Resources>
    <local:ObjectToColorConverter x:Key="ObjectToColorConverter">
        <local:ObjectToColorConverter.Colors>
            <local:StringToColorDictionary>
                <Color x:Key="A">Red</Color>
                <Color x:Key="B">Green</Color>
                <Color x:Key="C">Blue</Color>
            </local:StringToColorDictionary>
        </local:ObjectToColorConverter.Colors>
    </local:ObjectToColorConverter>
    <DataTemplate
        x:Key="ItemTemplate"
        x:DataType="local:Item">
        <TextBlock
            Foreground="{x:Bind Foo, Converter={StaticResource ObjectToColorConverter}}"
            Text="{x:Bind Foo}" />
    </DataTemplate>
</Page.Resources>

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

0
投票

另一种方法是将这个 FrameworkElement.DataContext 绑定到 view 元素。请参阅@mm8的回答https://stackoverflow.com/a/70703773。但似乎view不能是Microsoft.UI.Xaml.Window

我的测试:

<ResourceDictionary>
    <DataTemplate x:Key="MyDataTemplate" x:DataType="local:Customer">
        <StackPanel Orientation="Horizontal">
            <TextBlock local:AncestorSource.AncestorType="ListBox" Text="{Binding Tag}" />
            
            ...
        </StackPanel>
    </DataTemplate>
</ResourceDictionary>

<Window
    x:Class="App2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App2"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
        <ListBox ItemsSource="{x:Bind Customers}" Width="350" Margin="0,5,0,10" ItemTemplate="{StaticResource MyDataTemplate}" Tag="aTag"/>
    </StackPanel>
</Window>
© www.soinside.com 2019 - 2024. All rights reserved.