在XAML中添加ListviewItems时设置绑定值

时间:2014-06-17 01:32:01

标签: c# wpf xaml

如果标题有点偏,我很抱歉。不知道该怎么称呼它真的。 但这是我的问题:

我有一个带有自定义ItemsTemplate的ListView,它有一个Textblock和一个Image。两者都设置为从绑定中获取数据。

但这是我想知道的:

如果我想从XAML添加listviewItems:

<ListView HorizontalAlignment="Left" Width="150" ItemTemplate="{DynamicResource menuItems}" ItemsPanel="{DynamicResource menuLayout}">

<ListViewItem/> <---- Here
<ListViewItem/> <---- And here.
</ListView>

如何设置文本块和图像应具有的值?

<ListViewItem "TextBlockValue = asdasdasds"/> etc

这是Itemtemplate

<DataTemplate x:Key="menuItems">
            <Grid Width="150" Height="35">
                <Image Height="35" Width="35" HorizontalAlignment="Left"/>
                <TextBlock Text="{Binding Path=Text}" Margin="40,0,0,0" VerticalAlignment="Center" FontSize="15" FontWeight="Light" Foreground="White"/>
            </Grid>
        </DataTemplate>

1 个答案:

答案 0 :(得分:1)

简单且最灵活的解决方案

作为第一步,应该定义一个类作为一个ListViewItem的所有值的数据容器。

public class ItemData
{
    public ImageSource ImageSource { get; set; }
    public string Text { get; set; }
}

(从技术上讲,也可以在没有这样的自定义类的情况下执行此操作;但是,生成的XAML看起来会更复杂且可读性更低。)

绑定到 ItemData 元素属性的数据模板可能如下所示:

<DataTemplate x:Key="menuItems">
    <StackPanel Width="150" Height="35" Orientation="Horizontal">
        <Image Source="{Binding Path=ImageSource}" Height="35" Width="35" />
        <TextBlock Text="{Binding Path=Text}" Margin="5,0,0,0" VerticalAlignment="Center" FontSize="15" FontWeight="Light" Foreground="White"/>
    </StackPanel>
</DataTemplate>

ListView 的XAML中,将构建 ItemData 对象的集合并将其分配给 ListView.Items 属性。 ListView将相应地创建ListViewItem。

<ListView HorizontalAlignment="Left" Width="150" ItemsPanel="{DynamicResource menuLayout}" ItemTemplate="{StaticResource menuItems}">
    <ListView.Items>
        <My:ItemData ImageSource="x:\\path\\to\\Img_1.png" Text="Alpha" />
        <My:ItemData ImageSource="x:\\path\\to\\Img_2.png" Text="Beta" />
    </ListView.Items>
</ListView>


使用显式 ListViewItem 声明的解决方案

这个问题听起来像是一个寻求明确声明ListViewItems的解决方案。 但是,这不应该是推荐的方法,因为它只会导致更复杂的XAML,以实现与第一个解决方案完全相同的结果,如现在所示。

在ListView的XAML中,将构造每个ListViewItem以及相应的 ItemData 对象,并且还将数据模板分配给 ListViewItem.ContentTemplate (< em> ListView.ItemTemplate 在这里工作,因为ListViewItems不是由ListView创建的。

<ListView HorizontalAlignment="Left" Width="150" ItemsPanel="{DynamicResource menuLayout}">
    <ListViewItem ContentTemplate="{StaticResource menuItems}">
        <ListViewItem.Content>
            <My:ItemData ImageSource="x:\\path\\to\\Img_1.png" Text="Alpha" />
        </ListViewItem.Content>
    </ListViewItem>
    <ListViewItem ContentTemplate="{StaticResource menuItems}">
        <ListViewItem.Content>
            <My:ItemData ImageSource="x:\\path\\to\\Img_2.png" Text="Beta" />
        </ListViewItem.Content>
    </ListViewItem>
</ListView>


如果不希望为每个ListViewItem指定ContentTemplate,则可以在ListView的资源字典中定义ListViewItem Style 设置 ListViewItem.ContentTemplate 属性:

<ListView HorizontalAlignment="Left" Width="150" ItemsPanel="{DynamicResource menuLayout}">
    <ListView.Resources>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="ContentTemplate" Value="{StaticResource menuItems}" />
        </Style>
    </ListView.Resources>

    <ListViewItem>
        <ListViewItem.Content>
            <My:ItemData ImageSource="x:\\path\\to\\Img_1.png" Text="Alpha" />
        </ListViewItem.Content>
    </ListViewItem>
    <ListViewItem>
        <ListViewItem.Content>
            <My:ItemData ImageSource="x:\\path\\to\\Img_2.png" Text="Beta" />
        </ListViewItem.Content>
    </ListViewItem>
</ListView>

在ListView的资源字典中定义的样式会阻止此ListView之外的任何其他ListViewItem控件意外地拾取此样式。

将第一个解决方案的简单性与第二个解决方案的详细程度进行比较,很明显,XAML中的ListViewItems的显式声明不是推荐的方法。