绑定到ListView

时间:2015-11-11 09:16:33

标签: c# wpf xaml listview binding

我不知道我对绑定的理解是否很差,或者我是否没有看到问题,但我希望有人可以帮助我。我有一个ListView,其中包含图片模板和TextBlock,我需要将TextBlock绑定到ItemsSource的{​​{1}}。但是,当我运行此操作时,我什么都没有显示,我甚至没有看到我设置的ListView

XAML:

image

C#:

<UserControl.Resources>
    <FontFamily x:Key="FontFamily">MS Reference Sans Serif</FontFamily>        
</UserControl.Resources>

<Grid>
    <ListView BorderThickness="0" ItemsSource="{Binding Facies}">
        <ListView.ItemTemplate>
            <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Image Source="../Images/Shale.png"/>
                <TextBlock Text="{Binding FaciesName}" Width="75" Margin="5"/>
            </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

1 个答案:

答案 0 :(得分:1)

首先设置DataContext,如下所示:

public FaciesControl()
{
    InitializeComponent();
    string[] str = { "Name1", "Name2", "Name3" };
    Bind(str); // Make sure you have called the Bind method
    DataContext = Facies;
}

第二次改变你的XAML:

<ListView BorderThickness="0" ItemsSource="{Binding}">
 ....
 ....
<TextBlock Text="{Binding}" Width="75" Margin="5"/>