0

我在列表视图中创建了网格视图。在任何行上选择我想更新在列表视图之外创建的按钮上的文本。

提前致谢和问候

4

1 回答 1

0

尝试这样的事情:

Xaml 文件:

<Window x:Class="GVButton.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Button Content="{Binding ElementName=listView, Path=SelectedItem.Name}" />

        <ListView Background="Transparent" BorderThickness="0" Name="listView" Grid.Row="1">
            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                    <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.View>
                <GridView x:Name="grid">
                    <GridViewColumn Width="150" Header="ID" DisplayMemberBinding="{Binding ID}" />
                    <GridViewColumn Width="150" Header="Name" DisplayMemberBinding="{Binding Name}" />
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

代码隐藏文件:

using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;

namespace GVButton
{   
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            List<Foo> _source = new List<Foo>();
            for (int i = 0; i < 10; i++)
            {
                _source.Add(new Foo { ID = i, Name = "name_" + i });
            }

            listView.ItemsSource = _source;
        }
    }

    public class Foo
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
}
于 2012-07-30T21:56:24.523 回答