且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WPF DataTemplate Textblock绑定

更新时间:2022-05-02 02:00:51

如果我正确理解您的意思,您是在问如何设置文本块(当前位于DataTemplate中)的数据绑定?您不能在ListBox级别设置该数据绑定.它必须在您的DataTemplate中完成.

If I understand you correctly, you're asking how you set the databinding for the textblock, that is currently in your DataTemplate? You can't set that databinding at the ListBox level; it has to be done in your DataTemplate.

在这种情况下,DataTemplate将继承列表中每个项目的DataContext.

In this case the DataTemplate will inherit the DataContext of each item in the list.

<DataTemplate x:Key="myDataTemplate">
    <StackPanel>
       <TextBlock Text="{Binding Path=Name}" />
       <TextBlock Text="{Binding Path=AnotherListItemProperty}" />
    </StackPanel>
</DataTemplate>

换句话说-此DataTemplate是列表中每个项目的模板-而DataContext将是列表中每个项目.

In other words - this DataTemplate is a template for each item in the list - and the DataContext will be each item in the list.