如何绑定字符串表单的字符串列表?

时间:2014-09-12 07:03:11

标签: c# wpf data-binding listbox

以下是数据绑定无效的代码。 XAML

<ListBox x:Name="RecentBox" SelectionChanged="RecentBox_SelectionChanged" ItemsSource="{Binding}"  >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border BorderBrush="Red" BorderThickness="1" Background="Blue">
                        <TextBlock Text="{Binding RecentItemList}"/>
                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

现在是CS代码

public ObservableCollection<string> RecentItemsList { get { return new ObservableCollection<string>(((MainWindow)Application.Current.MainWindow).cacheFileList); } }
        public RecentItems()
        {
            InitializeComponent();
            RecentBox.ItemsSource = RecentItemsList;
        }

现在如何将字符串绑定到Textblock元素?

2 个答案:

答案 0 :(得分:2)

如果items source可枚举为字符串条目,请使用以下命令:

<TextBlock Text="{Binding}"></TextBlock> 

<强> XAML:

  <ListBox x:Name="RecentBox" SelectionChanged="RecentBox_SelectionChanged" ItemsSource="{Binding RecentItemList}"  >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border BorderBrush="Red" BorderThickness="1" Background="Blue">
                    <TextBlock Text="{Binding}"/>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

答案 1 :(得分:0)

最近的项目列表在Listbox itemssource属性中使用并使用如下:

<Border BorderBrush="Red" BorderThickness="1" Background="Blue"> <TextBlock Text="{Binding}"/> </Border>