繁体   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