当数据发生更改时,WPF ListBox中的绑定不会更新TextBox

时间:2016-05-26 18:36:13

标签: c# wpf xaml listbox

我有一份建筑物清单。建筑物有自己的类(core),并保存在ObservableCollection中。构建显示在列表中,但是当我更改列表中可见的变量时,该变量在xaml中不会更改。

以下是课程的来源:

public class core
    {
        // core ----------------------------------------------
        static public ObservableCollection<core> cores { get; set; } = new ObservableCollection<core>();
        string namef = "building";

public core()
        {
            cores.Add(this);
        }

public string Namef
        {
            get { return namef; }
            set { namef = value; }
        }
}

在wpf中 - xaml:

<Page x:Class="idle.pages.game"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:local="clr-namespace:idle.pages"
  mc:Ignorable="d" 
  d:DesignHeight="454.259" d:DesignWidth="757.012"
  Title="game">

<Grid Margin="0" x:Name="gridx">
    <Grid.RowDefinitions>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <TabControl x:Name="tabControl">
        <TabItem Header="Budovy">
            <Grid Background="#FFE5E5E5">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="300"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <ListBox x:Name="listBox">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid Margin="10,7" HorizontalAlignment="Stretch" Height="100">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="100" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                <Border Grid.Column="0" Grid.Row="0" Grid.RowSpan="3" CornerRadius="2"/>
                                <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding Namef}" />
                            </Grid>
                        </DataTemplate>
                   </ListBox.ItemTemplate>
                </ListBox>
                <Frame x:Name="frame" Content="Frame" Grid.Column="1" NavigationUIVisibility="Hidden" Source="/idle;component/pages/Building.xaml"/>


            </Grid>
        </TabItem>
    </TabControl>
</Grid>

并在wpf - c#:

public partial class game : Page
{
    public game()
    {
        InitializeComponent();
        new core() { Namef = "b1"};
        new core() { Namef = "b2"};
        new core() { Namef = "b3"};

        core.start(this);


        listBox.ItemsSource = xxx;
    }

    public ObservableCollection<core> xxx { get; set; }
}

我确定,类中的变量正在被更改,但是xaml没有。怎么了?

1 个答案:

答案 0 :(得分:0)

您需要实现INotifyPropertyChanged接口并在可以更改的所有属性的setter中调用PropertyChanged事件(并且您希望在UI中更新)。例如,如果您想查看Namef的更改,则必须按照以下方式实施:

public class core : INotifyPropertyChanged
{
    static public ObservableCollection<core> cores { get; set; }
        = new ObservableCollection<core>();

    string namef = "building";

    public core()
    {
        cores.Add(this);
    }

    public string Namef
    {
        get { return namef; }
        set 
        { 
            if(namef == value) return;

            namef = value; 
            OnPropertyChanged("Namef");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(String propertyName)
    {
        if (PropertyChanged == null) return;
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}