簡體   English   中英

更改數據時,WPF ListBox中的綁定不會更新TextBox

[英]Binding in WPF ListBox not updating TextBox, when data is changed

我有建築物清單。 建築物具有自己的類( 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未更改。 怎么了?

您需要實現INotifyPropertyChanged接口,並在所有可以更改的屬性的設置程序中調用PropertyChanged事件(並且您希望在UI中進行更新)。 例如,如果您想查看對Namef更改, 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));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM