繁体   English   中英

如果选择了网格行,如何更改按钮上的文本

[英]How change the text on button if grid row is selected

我已经在列表视图中创建了网格视图。 在任何行上,选择我要更新在列表视图外部创建的按钮上的文本。

预先感谢和问候

尝试这样的事情:

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; }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM