Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# uwp:如何在x:DataType之外的DataTemplate内绑定数据?_C#_Uwp_Combobox_Bind - Fatal编程技术网

C# uwp:如何在x:DataType之外的DataTemplate内绑定数据?

C# uwp:如何在x:DataType之外的DataTemplate内绑定数据?,c#,uwp,combobox,bind,C#,Uwp,Combobox,Bind,我有MyPage.xaml和MyPage.xaml.cs文件。 在.xaml文件中,我编写了如下数据模板: <DataTemplate x:Key="MyDataTemplate" x:DataType="local:MyClass"> ... <TextBlock Text="{x:Bind name}" HorizontalAlignment=&quo

我有MyPage.xaml和MyPage.xaml.cs文件。 在.xaml文件中,我编写了如下数据模板:

        <DataTemplate x:Key="MyDataTemplate" x:DataType="local:MyClass">
            ...
            <TextBlock Text="{x:Bind name}" HorizontalAlignment="Left" TextWrapping="Wrap"/>
            ...
        </DataTemplate>
<ComboBox ItemsSource="{x:Bind myList}" HorizontalAlignment="Left"></ComboBox>

...
...
我可以正确绑定MyClass的name属性。 现在我需要绑定.xaml.cs文件的属性,但DataTemplate只显示MyClass数据。如何从.xaml.cs页面绑定数据?在DataTemplate之外(但在同一个xaml文件中),我可以看到.xaml.cs文件的任何属性。 我需要将对象列表绑定到组合框,如下所示:

        <DataTemplate x:Key="MyDataTemplate" x:DataType="local:MyClass">
            ...
            <TextBlock Text="{x:Bind name}" HorizontalAlignment="Left" TextWrapping="Wrap"/>
            ...
        </DataTemplate>
<ComboBox ItemsSource="{x:Bind myList}" HorizontalAlignment="Left"></ComboBox>

但是myList是一个.xaml.cs属性。 我想查看列表中对象的字符串名称属性

谢谢你的帮助

uwp:如何在x:DataType之外的DataTemplate内绑定数据

对于您的重新创建,我们建议您使用
Binding
替换
x:Bind
,您可以使用
Binding
使用
ElementName
访问当前根数据上下文

比如说

<Grid x:Name="GridRoot">
    <ListView ItemsSource="{Binding Items}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}" />
                    <ComboBox ItemsSource="{Binding ElementName=GridRoot, Path=DataContext.Options}" />
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

代码隐藏

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        this.DataContext = this;
    }
    public List<string> Options { get; set; } = new List<string>() {"One","Two","Three" };
    public List<Item> Items { get; set; } = new List<Item>() 
    { 
        new Item { Name = "HH" },
        new Item { Name = "ZZ" }, 
        new Item { Name = "MM" }
    };
}
public class Item
{
    public string Name { get; set; }
}
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
this.DataContext=this;
}
公共列表选项{get;set;}=new List(){“一”、“二”、“三”};
公共列表项{get;set;}=new List()
{ 
新项目{Name=“HH”},
新项目{Name=“ZZ”},
新项目{Name=“MM”}
};
}
公共类项目
{
公共字符串名称{get;set;}
}

我通常使用WPF,所以我不知道它是否有效,但您可以尝试在页面中添加一个名称,如:
和组合框中:
ItemsSource=“{x:Bind myList ElementName=page}”
。如果答案解决了您的问题,请将其视为已接受