【问题标题】:Binding a TextBlock to a Property a ListView changes将 TextBlock 绑定到 ListView 更改的属性
【发布时间】:2013-01-09 18:31:30
【问题描述】:

我就是想不通。绑定Textblock 缺少什么? 每次我在ListView 中选择一个新项目时,我都需要更新TextBlock

这是我制作的样本。我是我的真实应用程序,我使用 ListView1 中的 id 从我的数据库中获取我想在我的 textBlock 中显示的内容..

我知道 WPF 绑定到属性,我需要实现 INotifyPropertyChanged,但我无法正确绑定,或者我可能缺少其他东西?

我添加了DateTime.Now.TosString() 只是为了更清楚地查看TextBlock 是否发生变化。

XAML:

<Window x:Class="WpfSO.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 />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Row="0" x:Name="txtBlockPerson" 
                                Text="{Binding MyPerson}" />


        <ListView Grid.Row="1" Grid.Column="0" x:Name="ListView1" 
                  ItemsSource="{Binding ListData}" 
                  IsSynchronizedWithCurrentItem="True" 
                  SelectionChanged="ListView1_SelectionChanged">

            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" Value="Left" />
                </Style>
            </ListView.ItemContainerStyle>

            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Name}"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>

C#

using System;
using System.ComponentModel;
using System.Windows;
using System.Collections.ObjectModel;

namespace WpfSO
{
    public partial class MainWindow : Window
    {
        private ObservableCollection<Person> ListData { get; set; }
        private const string _myName = "You clicked on: ";
        public Person MyPerson { get; set; }

        public MainWindow()
        {
            InitializeComponent();

            // TextBlock
            MyPerson = new Person(_myName);
            txtBlockPerson.DataContext = MyPerson;

            // ListView
            ListData = new ObservableCollection<Person>();
            var p1 = new Person("p1");
            var p2 = new Person("p2");
            ListData.Add(p1);
            ListData.Add(p2);

            ListView1.ItemsSource = ListData;
        }

        private void ListView1_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            MyPerson.Name = _myName + ListView1.SelectedItem + ". Time: " +DateTime.Now.ToString(); 
        }
    }

    public class Person : INotifyPropertyChanged
    {
        private string _name;
        public event PropertyChangedEventHandler PropertyChanged;

        public string Name
        {
            get { return _name; }
            set
            {
                if (value != _name)
                {
                    _name = value;
                    OnPropertyChanged("PersonName");
                }
            }
        }

        public Person(string name)
        {
            Name = name;
        }

        // Create the OnPropertyChanged method to raise the event 
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}

【问题讨论】:

    标签: c# wpf binding properties observer-pattern


    【解决方案1】:

    OnPropertyChanged 需要具有正确的属性名称..

    而不是OnPropertyChanged("PersonName"); 使用

    OnPropertyChanged("Name");
    

    【讨论】:

    • +1 哇,谢谢,我不知道。哦,我看到它必须有“名称”作为属性。第一次测试看起来有效。我会在更多测试后接受。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-12
    • 1970-01-01
    • 2017-11-18
    • 2014-01-28
    • 2018-10-24
    • 2015-07-24
    相关资源
    最近更新 更多